我想是的,但是
#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
没有通过调用它们的析构函数来正确销毁呢?
答案 0 :(得分:1)
c0
和d0
不是对象。你已经写了两个函数声明。