1

I thought yes, but

#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;
}

just prints (https://ideone.com/95DK9E)

28
bye C
30
bye D(42)

So why are c0 and d0 not properly destructed by a call to their destructor?

not-a-user
  • 3,898
  • 3
  • 18
  • 36

1 Answers1

1

c0 and d0 are not objects. You've written two function declarations.

Jesper Juhl
  • 28,933
  • 3
  • 44
  • 66