析构函数 [英] destructors
问题描述
如果你有:
#include< iostream>
使用命名空间std;
类基地
{
公共:
~Base()
{
cout<< "〜基地()" << endl;
}
int x;
};
class Derived :公共基地
{
公开:
~派生()
{
cout<< "〜衍生()" << endl;
}
int y;
int z;
};
int main()
{
Base * b = new Derived;
delete b;
}
只调用~Base(),这是预期的,因为它不是虚拟的。
但是,当调用delete时,它会释放分配给变量y和z的内存吗?
If you have:
#include <iostream>
using namespace std;
class Base
{
public:
~Base()
{
cout << "~Base()" << endl;
}
int x;
};
class Derived : public Base
{
public:
~Derived()
{
cout << "~Derived()" << endl;
}
int y;
int z;
};
int main()
{
Base* b = new Derived;
delete b;
}
Only ~Base() is called, which is expected since it is not virtual.
However, when delete is called, will it free the memory allocated for
the variables y and z?
推荐答案
10月15日,12:20 * am,cron ... @ gmail.com写道:
On Oct 15, 12:20*am, cron...@gmail.com wrote:
如果你有:
#include< iostream> ;
使用命名空间std;
* class Base
{
public :
* * ~Base()
* * * * {
* * * * * * * * cout<< "〜基地()" << endl;
* * * *}
* * int x;
};
class派生:公共基地
{
公开:
* * * *〜派生()
* * * * {
* * * * * * * * cout<< "〜衍生()" << endl;
* * * *}
* * int y;
* * int z;
< br $>
};
int main()
{
* * Base * b = new Derived ;
* *删除b;
}
只调用~Base(),这是预期的因为它不是虚拟的。
然而,当调用delete时,是否会释放分配给
变量y和z的内存?
If you have:
#include <iostream>
using namespace std;
*class Base
{
public:
* * ~Base()
* * * * {
* * * * * * * * cout << "~Base()" << endl;
* * * * }
* * int x;
};
class Derived : public Base
{
public:
* * * * ~Derived()
* * * * {
* * * * * * * * cout << "~Derived()" << endl;
* * * * }
* * int y;
* * int z;
};
int main()
{
* * Base* b = new Derived;
* * delete b;
}
Only ~Base() is called, which is expected since it is not virtual.
However, when delete is called, will it free the memory allocated for
the variables y and z?
是
Yes
10月15日,12:24 * pm,george.p ... @ gmail .com写道:
On Oct 15, 12:24*pm, george.p...@gmail.com wrote:
10月15日,12:20 * am,cron ... @ gmail.com写道:
On Oct 15, 12:20*am, cron...@gmail.com wrote:
如果你有:
If you have:
#include< iostream>
#include <iostream>
using namespace std;
using namespace std;
* class Base
{
public:
* * ~Base()
* * * * {
* * * * * * * * cout<< "〜基地()" << endl;
* * * *}
*class Base
{
public:
* * ~Base()
* * * * {
* * * * * * * * cout << "~Base()" << endl;
* * * * }
* * int x;
* * int x;
};
};
class派生:公共基地
{
public:
* * * *〜派生()
* * * * {
* * * * * * * * cout<< "〜衍生()" << endl;
* * * *}
class Derived : public Base
{
public:
* * * * ~Derived()
* * * * {
* * * * * * * * cout << "~Derived()" << endl;
* * * * }
* * int y;
* * int z;
* * int y;
* * int z;
};
};
int main()
{
* * Base * b = new Derived;
* *删除b;
int main()
{
* * Base* b = new Derived;
* * delete b;
}
}
只调用~Base(),其中因为它不是虚拟的,所以是预期的。
然而,当调用delete时,是否会释放分配给变量y和z的内存的内存?
Only ~Base() is called, which is expected since it is not virtual.
However, when delete is called, will it free the memory allocated for
the variables y and z?
是
Yes
我不这么认为。
To自由y和z你必须改变~Base()到虚拟。
I don''t think so.
To free y and z you must change ~Base() to virtual.
cr * **** @gmail.com 写道:
cr*****@gmail.com wrote:
如果你有:
#include< iostream>
使用命名空间std;
class Base
{
public :
~Base()
{
cout<< "〜基地()" << endl;
}
int x;
};
class Derived :公共基地
{
公开:
~派生()
{
cout<< "〜衍生()" << endl;
}
int y;
int z;
};
int main()
{
Base * b = new Derived;
delete b;
}
只调用~Base(),这是预期的,因为它不是虚拟的。
但是,当调用delete时,它会释放为变量y和z分配的内存吗?
If you have:
#include <iostream>
using namespace std;
class Base
{
public:
~Base()
{
cout << "~Base()" << endl;
}
int x;
};
class Derived : public Base
{
public:
~Derived()
{
cout << "~Derived()" << endl;
}
int y;
int z;
};
int main()
{
Base* b = new Derived;
delete b;
}
Only ~Base() is called, which is expected since it is not virtual.
However, when delete is called, will it free the memory allocated for
the variables y and z?
调用delete时,根据[5.3.5 / 3]有未定义的行为。特别是
,关于变量y
和z的内存,所有投注均已关闭。
最佳
Kai-Uwe Bux
When delete is called you have undefined behavior as per [5.3.5/3]. In
particular, all bets are off with respect to the memory of the variables y
and z.
Best
Kai-Uwe Bux
这篇关于析构函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!