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. 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
     
    Victor Bazarov, Jun 11, 2014
    #2
    1. Advertisements

  3. K' Dash

    K' Dash Guest


    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

    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
     
    Pavel, Jun 15, 2014
    #4
    1. Advertisements

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