【发布时间】:2018-01-23 21:41:40
【问题描述】:
以下是我的代码摘要:
基类:
#include <iostream>
using namespace std;
class Base {
public:
Base() { cout << "Base constructor" << endl; }
~Base() { cout << "Base destructor" << endl; }
virtual void func(void) const { cout << "base" << endl; }
};
派生类:
#include "Base.h"
class Derived : public Base {
public:
Derived() { cout << "Derived constructor" << endl; }
~Derived() { cout << "Derived destructor" << endl; }
void func(void) const { cout << "derived" << endl; }
};
测试类:
#include "Derived.h"
class Test {
public:
const Base& base;
Test(const Base& _base) : base(_base) { cout << "Test constructor" << endl; }
void test() { base->func(); }
~Test() { cout << "Test destructor" << endl; }
};
测试的主要功能:
#include "Test.h"
int main(void) {
Test* t = new Test(Derived());
t->test();
return 0;
}
当我运行main函数时,func的Base版本被调用。
但是,如果我将 main 函数更改为以下内容:
#include "Test.h"
int main(void) {
Derived d;
Test* t = new Test(d);
t->test();
return 0;
}
func 的Derived 版本被正确调用。 我还尝试将 Test 中的 const Base& base 更改为 Base* base。然后使用
构造 TestTest* t = new Test(new Derivec())
事实证明,func的Derived版本也被正确调用了。
我在想,如果我使用引用或指针,多态性就会起作用。
谁能解释一下为什么第一个版本没有正确调用派生类方法?
非常感谢您的帮助!
【问题讨论】:
-
首先,这段代码有未定义的行为。其次,这段代码不应该编译。第三,这段代码在多态类中有非虚析构函数。
-
@SergeyA:不用担心虚拟析构函数,没有
delete:-/ -
对不起!非虚拟析构函数是一个错字...
标签: c++ c++11 inheritance polymorphism virtual-functions