由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 用包子呼唤大牛们--问关于C++Destructor的问题
相关主题
[合集] C++问题(copy constructor)[合集] C++ interview question help
请教个Bloomberg 的 C++ 题目急问:compile and build dependency
一个c++ constructor的问题, thanks请教一下,exception时,destructor一定会被调用么?
菜鸟请教smart pointerquestion regarding effective c++ by Meyers
C++ online Test 2题private destructor
请教一个基本的constructor和destrcutor问题c++ initialize struct
请教这个程序里用到了什么constructor啊?有几个copy constructor?我真不明白c++有什么难的
A try-catch problem in C++C++ 中 myobject * a =new myobject[n] 的问题
相关话题的讨论汇总
话题: base话题: derived话题: var话题: var2话题: destructor
进入Programming版参与讨论
1 (共1页)
w********p
发帖数: 948
1
#include
using namespace std;
class Base
{
public:
Base(){ cout<<"Constructor: Base"< ~Base(){ cout<<"Destructor : Base"< };
class Derived: public Base
{
//Doing a lot of jobs by extending the functionality
public:
Derived(){ cout<<"Constructor: Derived"< ~Derived(){ cout<<"Destructor : Derived"< };
int main()
{
Derived *Var = new Derived();

Base Var2;
Var2 = (Base) *Var;
delete Var;
return 0;
}
output:
Constructor: Base //1
Co
X****r
发帖数: 3557
2
Derived *Var = new Derived();
// a. calls Base::Base() (1)
// b. calls Derived::Derived() (2)
Base Var2;
// calls Base::Base() (3)
Var2 = (Base) *Var;
// a. calls default copy constructor Base::Base(const Base&)
// to create a temporary Base object from *Var
// b. calls default assignment operator
// Base::operator =(const Base&) to assign this temporary
// object to Vars
// c. calls Base::~Base() for the temporary object. (4)
delete Var;
// a. calls Derived::~Derived() (5)
// b. calls Base::~Ba
H***a
发帖数: 735
3
From Var2. When you exit main(), it will automatically call Base's
destructor to clean up the memory on stack.
w********p
发帖数: 948
4
谢谢大牛。请问那本书上会解释道(4)
小小typo
// Base::operator ==(const Base&) to assign this temporary
应该是
// Base::operator =(const Base&) to assign this temporary
w********p
发帖数: 948
5
you are right.
I did not understand what happen on " Var2 = (Base) *Var;"

【在 H***a 的大作中提到】
: From Var2. When you exit main(), it will automatically call Base's
: destructor to clean up the memory on stack.

X****r
发帖数: 3557
6
对,我打错了,改过来了。
哪本书的话……你学到了东西以后还会记住这些东西都是从哪里学来的吗?:)

【在 w********p 的大作中提到】
: 谢谢大牛。请问那本书上会解释道(4)
: 小小typo
: // Base::operator ==(const Base&) to assign this temporary
: 应该是
: // Base::operator =(const Base&) to assign this temporary

t****u
发帖数: 8614
7
4 is due to Var2 = (Base) *Var;
invoke an operator=(), you don't have it, so this is member-wise copy. A
temp object is still constructed through assignment operator, instead of
using default ctor. Since you slice the object, you only see the dtor of
base class.
5,6 is due to delete
7 is the "Base Var2;"
When out of the method scope, the dtor is called.

【在 w********p 的大作中提到】
: #include
: using namespace std;
: class Base
: {
: public:
: Base(){ cout<<"Constructor: Base"<: ~Base(){ cout<<"Destructor : Base"<: };
: class Derived: public Base
: {

w********p
发帖数: 948
8
谢谢牛牛们,欢迎继续指教,包子到此为止
z***e
发帖数: 5393
9
如果你是用reference or pointer,就不会有temporary value.
但是 Var2 = (Base) *Var;
就差不多相当于:
{
Base base;
memcpy(&base, Var, sizeof(base));
call Var2.Base(base);
}
你给Base 自己加一个 Base(const Base&)随便打点东西出来就清楚了。

【在 w********p 的大作中提到】
: you are right.
: I did not understand what happen on " Var2 = (Base) *Var;"

1 (共1页)
进入Programming版参与讨论
相关主题
C++ 中 myobject * a =new myobject[n] 的问题C++ online Test 2题
C++里面请教一个基本的constructor和destrcutor问题
[合集] 关于C++ default copy constructor请教这个程序里用到了什么constructor啊?有几个copy constructor?
关于c++的constructor的面试题A try-catch problem in C++
[合集] C++问题(copy constructor)[合集] C++ interview question help
请教个Bloomberg 的 C++ 题目急问:compile and build dependency
一个c++ constructor的问题, thanks请教一下,exception时,destructor一定会被调用么?
菜鸟请教smart pointerquestion regarding effective c++ by Meyers
相关话题的讨论汇总
话题: base话题: derived话题: var话题: var2话题: destructor