Virtual destructor

Discussion in 'C++' started by K' Dash, Jun 11, 2014.

  1. K' Dash

    K' Dash Guest

    dear members

    I have a question. If a base class have a virtual destructor, then is it necessary to write in child class (like as we write pure virtual function in child class). If yes then how can we write in child class?

    e.g

    class Ipv4RoutingHelper
    {
    public:

    virtual ~Ipv4RoutingHelper ();

    virtual Ipv4RoutingHelper* Copy (void) const = 0;
    virtual Ptr<Ipv4RoutingProtocol> Create (Ptr<Node> node) const = 0;

    **********************************************************************

    class MeDeHaDtnRoutingHelper : public Ipv4RoutingHelper
    {
    public:
    MeDeHaDtnRoutingHelper ();
    ~MeDeHaDtnRoutingHelper ();

    virtual Ptr<Ipv4RoutingProtocol> Create (Ptr<Node> node) const;
    virtual Ipv4RoutingHelper* Copy (void) const;
     
    K' Dash, Jun 11, 2014
    #1
    1. Advertisements

  2. On 6/11/2014 4:05 PM, K' Dash wrote:
    > I have a question. If a base class have a virtual destructor, then
    > is

    it necessary to write in child class (like as we write pure virtual
    function in child class). If yes then how can we write in child class?

    Not necessary. If your derived class has a trivial d-tor (nothing
    special to do), it's still going to be declared 'virtual' and work
    correctly. The compiler will "create" the d-tor for you and call it
    when destroying the objects of your derived type.

    > [..]


    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jun 11, 2014
    #2
    1. Advertisements

  3. K' Dash

    K' Dash Guest

    On Thursday, June 12, 2014 1:33:34 AM UTC+5, Paavo Helde wrote:
    > "K' Dash" <> wrote in
    >
    > news::
    >
    >
    >
    > > dear members

    >
    > >

    >
    > > I have a question. If a base class have a virtual destructor, then is

    >
    > > it necessary to write in child class (like as we write pure virtual

    >
    > > function in child class).

    >
    >
    >
    > No, a virtual function needs to be overridden in the child classes only if
    >
    > it is pure, but the destructor cannot be really pure anyway, so the
    >
    > question is moot. So if the child class does not need special destruction
    >
    > it does not need to define a destructor.
    >
    >
    >
    > On the other hand, if any of derived classes requires specific destruction
    >
    > it must provide the destructor override; declaring it virtual in the base
    >
    > class will then ensure it will be called properly in all circumstances.
    >
    >
    >
    > > If yes then how can we write in child class?

    >
    >
    >
    > Like a regular destructor.
    >
    >
    >
    > hth
    >
    > Paavo



    thanks friend.

    I solved the problem.but here another error raised.

    ../libns3.19-medeha-debug.so: undefined reference to `ns3::MeDeHaDtnRoutingHelper::Copy() const'
    collect2: error: ld returned 1 exit status
    Waf: Leaving directory `/home/adnan/porting/ns-allinone-3.19/ns-3.19/build'
    Build failed


    medeha-dtn-routing.h file is inherited from ipv4-routing-helper.h. the base class have two pure virtual function and I wrote them in child class, after reading your replies. but I don't know why this error stops creating libns3.19-medeha-debug.so.

    plz guide me. thanx


    Regards
    K'Dash
     
    K' Dash, Jun 11, 2014
    #3
  4. K' Dash

    Pavel Guest

    Paavo Helde wrote:
    > "K' Dash" <> wrote in
    > news::
    >
    >> dear members
    >>
    >> I have a question. If a base class have a virtual destructor, then is
    >> it necessary to write in child class (like as we write pure virtual
    >> function in child class).

    >
    > No, a virtual function needs to be overridden in the child classes only if
    > it is pure, but the destructor cannot be really pure anyway,

    I think you can declare the destructor pure virtual. It is actually not a bad
    way to mark a class abstract (that is, to prevent the client code from
    instantiating it). Of course, to create instanti-able derived classes, you will
    need to define that destructor, too.

    -Pavel

    so the
    > question is moot. So if the child class does not need special destruction
    > it does not need to define a destructor.
    >
    > On the other hand, if any of derived classes requires specific destruction
    > it must provide the destructor override; declaring it virtual in the base
    > class will then ensure it will be called properly in all circumstances.
    >
    >> If yes then how can we write in child class?

    >
    > Like a regular destructor.
    >
    > hth
    > Paavo
    >
     
    Pavel, Jun 15, 2014
    #4
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. Stub

    Virtual Destructor

    Stub, Nov 6, 2003, in forum: C++
    Replies:
    11
    Views:
    1,093
    Dan Cernat
    Nov 10, 2003
  2. Paul McKenzie
    Replies:
    2
    Views:
    378
    Rolf Magnus
    Nov 22, 2003
  3. Calvin Lai
    Replies:
    7
    Views:
    838
    Calvin Lai
    Dec 18, 2003
  4. Chunhui Han
    Replies:
    2
    Views:
    825
  5. frs
    Replies:
    20
    Views:
    1,230
    Alf P. Steinbach
    Sep 21, 2005
  6. arun
    Replies:
    2
    Views:
    866
    benben
    Jun 13, 2006
  7. Jimmy Hartzell
    Replies:
    0
    Views:
    724
    Jimmy Hartzell
    May 19, 2008
  8. Jimmy Hartzell
    Replies:
    2
    Views:
    1,460
    Jimmy Hartzell
    May 20, 2008
Loading...