virtual destructor

P

Pradeep Patra

Hi all,
If I have the following situation: I have a class A which doesnot
have any virtual functions . If I will declare the destructor as
virtual will that cause any problem? like memory leak etc?

Class B;
class A
{
public:
A();
virtual ~A();

private:
B* b;

}
 
A

Alf P. Steinbach

* Pradeep Patra:
Hi all,
If I have the following situation: I have a class A which doesnot
have any virtual functions . If I will declare the destructor as
virtual will that cause any problem? like memory leak etc?

No.


Cheers & hth.,

- Alf
 
J

Juha Nieminen

Pradeep said:
If I have the following situation: I have a class A which doesnot
have any virtual functions . If I will declare the destructor as
virtual will that cause any problem? like memory leak etc?

I'm curious to know why you think making the destructor virtual could
cause a memory leak.
 
Y

Yan

If I will declare the destructor as
virtual will that cause any problem? like memory leak etc?

no. on the contrary, leaving your destructor non-virtual may cause
memory leaks. the only downside of declaring your destructor virtual
(since that would be the first virtual method in the class) is that
most likely the size of the objects of this class will become larger
by the size of the virtual pointer. I doubt it matters in 99.99% of
the cases though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top