简单虚函数
//============================================================================
// Name : p1022VirtualDeconstructor.cpp
// Author : perfey
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
using namespace std;
class Base
{
public:
Base(){cout << "Conctructing Base!"<<endl;}
virtual ~Base(){cout << "Deconstructing Base!"<<endl;}//多一个空格,virtual和~之间
};
class Derive: public Base
{
public:
Derive(){cout << "Constructing Derive!"<<endl;}
~ Derive(){cout << "Deconstructing Derive!"<<endl;}
/*
* 若是不加virtual,只输出构建base, Derive, 解构Base 三步,没有解构 derive? 这个有问题了
*
* */
};
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
Base *bptr = new Derive(); //父类指针指向之类实例;
delete bptr;
return 0;
}
结果
Conctructing Base!
Constructing Derive!
Deconstructing Derive!
Deconstructing Base!
(如果没有 tilde~前面那个virtual修饰,那么不会有Deconstructing Derive!这一句)
继续传递,继承下去
//============================================================================
// Name : p1022VirtualDeconstructor.cpp
// Author : perfey
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
using namespace std;
class Base
{
public:
Base(){cout << "Conctructing Base!"<<endl;}
virtual ~Base(){cout << "Deconstructing Base!"<<endl;}//多一个空格,virtual和~之间
};
class Derive: public Base
{
public:
Derive(){cout << "Constructing Derive!"<<endl;}
~ Derive(){cout << "Deconstructing Derive!"<<endl;}
/*
* 若是不加virtual,只输出构建base, Derive, 解构Base 三步,没有解构 derive? 这个有问题了
*
* */
};
class Derive2: public Derive
{
public:
Derive2(){cout << "Constructing DeriveSon!"<<endl;}
~ Derive2(){cout << "Deconstructing DeriveSon!"<<endl;}
/*
* 若是不加virtual,只输出构建base, Derive, 解构Base 三步,没有解构 derive? 这个有问题了
*
* */
};
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
Base *bptr = new Derive2(); //父类指针指向之类实例;
delete bptr;
return 0;
}
结果
!!!Hello World!!!
Conctructing Base!
Constructing Derive!
Constructing DeriveSon!
Deconstructing DeriveSon!
Deconstructing Derive!
Deconstructing Base!
可以看到每一个子类都继承虚函数,都调用了Deconstructor这是预期的效果。
继续花样
//============================================================================
// Name : p1022VirtualDeconstructor.cpp
// Author : perfey
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
using namespace std;
class Base
{
public:
Base(){cout << "Conctructing Base!"<<endl;}
virtual ~Base(){cout << "Deconstructing Base!"<<endl;}//多一个空格,virtual和~之间
virtual void Eat() = 0; //纯需函数,无需在基类中实现,奇怪吧,类似interface中概念
};
class Derive: public Base
{
public:
Derive(){cout << "Constructing Derive!"<<endl;}
void Eat()override
{
cout <<" 我吃生肉,茹毛饮血!!"<<endl;
}
~ Derive(){cout << "Deconstructing Derive!"<<endl;}
};
/*
* 若是不加virtual,只输出构建base, Derive, 解构Base 三步,没有解构 derive? 这个有问题了
*
* */
class Derive2: public Derive
{
public:
Derive2(){cout << "Constructing DeriveSon!"<<endl;}
~ Derive2(){cout << "Deconstructing DeriveSon!"<<endl;}
void Eat() {
cout <<" 我今晚大口吃肉!"<<endl;
}
};
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
Base *bptr = new Derive(); //父类指针指向之类实例;
bptr->Eat();
bptr = new Derive2();
bptr->Eat();
delete bptr;
return 0;
}
纯虚函数来结果
!!!Hello World!!!
Conctructing Base!
Constructing Derive!
我吃生肉,茹毛饮血!!
Conctructing Base!
Constructing Derive!
Constructing DeriveSon!
我今晚大口吃肉!
Deconstructing DeriveSon!
Deconstructing Derive!
Deconstructing Base!
本文详细探讨了C++中虚函数的使用,包括虚析构函数的作用,以及如何确保正确调用子类的析构。通过实例展示了不加virtual关键字时的异常行为,并深入剖析了基类指针指向派生类对象时的内存管理。
1678

被折叠的 条评论
为什么被折叠?



