Calling virtual functions inside constructors

前端 未结 13 1093

Suppose I have two C++ classes:

class A
{
public:
  A() { fn(); }

  virtual void fn() { _n = 1; }
  int getn() { return _n; }

protected:
  int _n;
};

clas         


        
13条回答
  •  Happy的楠姐
    2020-11-21 06:09

    Do you know the crash error from Windows explorer?! "Pure virtual function call ..."
    Same problem ...

    class AbstractClass 
    {
    public:
        AbstractClass( ){
            //if you call pureVitualFunction I will crash...
        }
        virtual void pureVitualFunction() = 0;
    };
    

    Because there is no implemetation for the function pureVitualFunction() and the function is called in the constructor the program will crash.

提交回复
热议问题