Virtual destructor


K

K' Dash

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;
 
Ad

Advertisements

V

Victor Bazarov

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
 
K

K' Dash

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.






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
 
Ad

Advertisements

P

Pavel

Paavo said:
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
 

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

Top