当对象脱离范围时,C 会调用击曲线吗?

Does C++ call the destructor when an object goes out of scope?

本文关键字:调用 曲线 对象 范围      更新时间:2023-10-16

我以为是的,但是

#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)

那么,为什么c0d0没有被呼叫对灾难的呼叫销毁?

c0d0不是对象。您已经写了两个函数声明。