virtual destructor

Discussion in 'C++' started by Pradeep Patra, Jul 3, 2009.

  1. 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;

    }
     
    Pradeep Patra, Jul 3, 2009
    #1
    1. Advertising

  2. * 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
     
    Alf P. Steinbach, Jul 3, 2009
    #2
    1. Advertising

  3. Pradeep Patra wrote:
    > 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.
     
    Juha Nieminen, Jul 3, 2009
    #3
  4. Pradeep Patra

    Yan Guest

    On Jul 3, 7:53 am, Pradeep Patra <> wrote:
    > Hi all,


    > 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.
     
    Yan, Jul 3, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Calvin Lai
    Replies:
    7
    Views:
    574
    Calvin Lai
    Dec 18, 2003
  2. Chunhui Han
    Replies:
    2
    Views:
    531
  3. frs
    Replies:
    20
    Views:
    793
    Alf P. Steinbach
    Sep 21, 2005
  4. arun
    Replies:
    2
    Views:
    577
    benben
    Jun 13, 2006
  5. Jimmy Hartzell
    Replies:
    0
    Views:
    439
    Jimmy Hartzell
    May 19, 2008
Loading...

Share This Page