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
  • 4,088
  • 3
  • 21
  • 37

1 Answers1

1

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

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70