先说结果: 父类定义的虚函数,子类如果重载了 ,那就必须定义具体实现,不能弄个没有函数体的函数名放那里碰瓷。
#include<iostream>
using namespace std;
class parent
{
public:
parent(){
std::cout<<"parent constructor no param"<<std::endl;
};
private:
parent(int a){
std::cout<<"parent constructor: "<<a<<std::endl;
};
public:
virtual ~parent(){
std::cout<<"parent disconstructor~"<<std::endl;
};
virtual void opt(){
std::cout<<"parent opt"<<std::endl;
};
};
class child:public parent
{
public:
child(int a){
cout<<"child constructor: "<<a<<endl;
};
~child();
void opt(){
std::cout<<"child opt"<<std::endl;
};
void child_do(){
std::cout<<"child do"<<std::endl;
};
};
int main()
{
int b = 10;
parent* test = new child(b);
test->opt();
reinterpret_cast<child*>(test)->child_do();
delete parent;
return 0;
}
/tmp/cc3sxtLw.o: In function `child::child(int)':
test.cpp:(.text._ZN5childC2Ei[_ZN5childC5Ei]+0x1d): undefined reference to `vtable for child'
找了好一会,发现是子类的析构函数只是声明了,没有定义,
改成 ~child(){}; 定义上就好了。
但是我记得之前也有过忘记定义析构函数具体内容的情况,编译并没有报错,
于是回过头注意到 这句报错的含义好像并不是说子类的析构函数没有定义,
undefined reference to `vtable for child' -> 对child的虚表未定义的引用,
这里 vtable 应该就是指的虚函数表,
尝试着换一种方式修改 ~child(); 这个不变, 把 virtual ~parent 改为 ~parent,
果然 也不报错了,但是我们都知道这样改的后果是啥,就是父类指针指向的子类对象析构不会调用子类的析构函数了。
[root@iZuf6iemoke3l0asocwqo5Z 继承]# ./a.out
parent constructor no param
child constructor: 10
child opt
child do
parent disconstructor~
[root@iZuf6iemoke3l0asocwqo5Z 继承]#
所以,老老实实给父类的析构函数加上 virtual, 然后完善对子类析构函数的定义.
virtual ~parent(){
std::cout<<"parent disconstructor~"<<std::endl;
};
~child(){
cout<<"child disconstructor"<<endl;
};