C ++父类调用子虚函数 [英] C++ Parent class calling a child virtual function

查看:102
本文介绍了C ++父类调用子虚函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个纯虚拟父类来调用这样的函数的子实现:

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;
}

这应该可以工作,但我得到一个未链接的错误:/这是使用VC ++ 2k3

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

或者不应该工作,我错了吗?

Or shouldn't it work, am I wrong?

推荐答案

p>以下文章的标题说明了这一切:在构建或销毁过程中切勿调用虚拟函数

Title of the following article says it all: Never Call Virtual Functions during Construction or Destruction.

这篇关于C ++父类调用子虚函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆