C++ Parent class calling a child virtual function

2019-03-25 08:03发布

I want a pure virtual parent class to call a child implementation of a function like so:

class parent
{
  public:
    void Read() { //read stuff }
    virtual void Process() = 0;
    parent() 
    {
        Read();
        Process();
    }
}
class child : public parent
{
  public:
    virtual void Process() { //process stuff }
    child() : parent() { }
}

int main()
{
   child c;
}

This should work, but I get an unlinked error :/ This is using VC++ 2k3

Or shouldn't it work, am I wrong?

7条回答
等我变得足够好
2楼-- · 2019-03-25 09:01

Will work in general, but not for calls within the constructor of the pure virtual base class. At the time the base class in constructed, the sub-class override doesn't exist, so you can't call it. As long as you call it once the entire object is constructed, it should work.

查看更多
登录 后发表回答