Mark
Mark

Reputation: 419

standard destructor when a destructor is redefined

Is it possible to call the default destructor even if I have redefined a destructor for a class in C++? for example if I have two classes

class B{
  ...
};

class A{
private:
   B* p;
public:
   A(B b):p(new B(b)){...}
   ...
   ~A(){delete p;}
};

and I don't always want to delete the object pointed by p.

Upvotes: 0

Views: 266

Answers (2)

Ixrec
Ixrec

Reputation: 976

Assuming by "standard destructor" you mean the one your compiler would have generated by default, the answer is no. Because you defined your own destructor, the compiler simply does not generate a default one. Destructors aren't meant to be "called" like a normal function anyway (unless you're implementing something like an STL container).

If you need your destructor to do different things under different circumstances, put an if() in the destructor, and have the object keep track of whatever information it needs for the if() condition. That's probably a better design anyway.

Upvotes: 6

brunodd
brunodd

Reputation: 634

A destructor has only 1 possible signature, so you can't define multiple versions.

Upvotes: 2

Related Questions