如何从c ++中的基类构造函数调用派生类方法? [英] How to call derived class method from the base class constructor in c++?

查看:222
本文介绍了如何从c ++中的基类构造函数调用派生类方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基类和两个派生类。基类构造函数应在调用时计算一些属性,但这些属性取决于派生类详细信息。为了避免在每个派生类构造函数中重新编译相同的步骤,我在基类构造函数中编写这些步骤,如下面的示例所示。

I have a base class and two derived classes. The base class constructor should calculate some properties when it is called, although those properties depend on the derived class details. To avoid recoding the same steps inside each derived class constructor, I code these steps in the base class constructor like in the example below.

问题是, ,基类构造函数不调用重写的方法。相反,它调用自己的方法。有办法解决这个问题吗?虽然这也是有意义的,这个行为发生的原因是什么?

The problem is that when I do this, the base class constructor do not call the overridden methods. Instead, it calls its own method. Is there a way to solve that? Although it makes sense as well, what's the reason this behavior happens?

来自C#背景,这很奇怪,因为它会很好地工作。在C#中,我将使用关键字 base 来调用任何基类方法,而这个将总是调用派生类方法。

Coming from a C# background, that's very odd, since it would work nicely there. In C#, I would use the keyword base to call any base class method whereas this would always call the derived class methods.

示例:

example.h

#define _USE_MATH_DEFINES
#include <math.h>

class Base
{
public:
    Base(void);
    ~Base(void);
protected:
    virtual void Method(void);
};
class Derived : public Base
{
public:
    Derived(void);
    ~Derived(void);
protected:
    virtual void Method(void);
};

example.cpp

#include <iostream>

Base::Base()
{
    this->Method(); // this calls Base->Method instead of Derived->Method
}
Base::~Base(){}
void Base::Method() // if I remove this, I have an error "externals undefined"
{
    std::cout << "called Base->Method()" << endl;
}

Derived::Derived()
    : Base()
{
    this->Method(); // this obviously calls Derived->Method
}
Derived::~Derived(){}
void Derived::Method()
{
    std::cout << "called Derived->Method()" << endl;
}

main.cpp
$ b

main.cpp

int main()
{
    Base* d = new Derived();
    /*
    outputs:
        called Base->Method()
        called Derived->Method()
    */
}


推荐答案

没有办法,因为当基类构造函数是运行时,对象还不是派生类型的对象。特别地,在派生类中引入的数据成员不会被初始化,直到基本类构造函数运行 - 基本类对象的行为像派生类的数据成员。

There is no way to do that, because when the base class constructor is run, the object is not yet an object of the derived type. In particular, data members introduced in the derived class are not initialised until after the base class constructor is run -- in essence, the base class object behaves like a data member of the derived class.

一种或另一种方式,你必须推迟计算,直到输入派生类的构造函数。如果我明白你正在做什么正确,最好的方法是给基类一个成员函数执行计算和调用从派生类的构造函数。

One way or another, you will have to defer the calculation until the derived class's constructor is entered. If I understand what you're trying to do correctly, the best way is probably to give the base class a member function that does the calculations and call that from the derived classes' constructors.

这篇关于如何从c ++中的基类构造函数调用派生类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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