当对象脱离范围时,C 会调用击曲线吗?
Does C++ call the destructor when an object goes out of scope?
我以为是的,但是
#include <iostream>
struct S {
int t;
};
class C {
private:
S s;
public:
C() {s.t = 7;}
~C(){std::cout << "bye C" << std::endl;}
};
class D {
private:
S s;
public:
D(int t) {s.t = t;}
~D() {std::cout << "bye D(" << s.t << ")" << std::endl;}
};
int main() {
C c0();
C* c1 = new C();
D d0();
D d1(42);
std::cout << __LINE__ << std::endl;
delete c1;
std::cout << __LINE__ << std::endl;
}
只需打印(https://ideone.com/95dk9e(
28
bye C
30
bye D(42)
那么,为什么c0
和d0
没有被呼叫对灾难的呼叫销毁?
c0
和 d0
不是对象。您已经写了两个函数声明。
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 函数调用中参数的顺序重要吗
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在c++类上调用void函数
- 为什么 std::unique 不调用 std::sort?
- 调用专用模板时出错"no matching function for call to [...]"
- 选择要调用的构造函数
- C++为什么尽管我调用了void函数,它却不起作用
- 构造函数正在调用一个使用当前类类型的函数
- 变量没有改变?通过向量的函数调用
- 没有为自己的结构调用列表推回方法
- 调用'begin(int [n])'没有匹配函数
- 什么时候调用析构函数
- 当对象脱离范围时,C 会调用击曲线吗?
- 即使在同一类型上,也可以将击曲线调用新的位置
- c++.将定义了2个点的绘图贝塞尔曲线调用转换为定义了3个点的调用