About virtual destructor

S

sam

Hi,
See when i reading a sourcecode of a program,
I read that the constructor is ordinary and after that the programmer
has written virtual destructor for that constructor .
Why we use the virtual destructor whats the use of it?
the code is like this:

Network(int input,int output);
Network(&Network);
virtual ~Network();

My question is why he use virtual destructor.
 
K

kwikius

sam said:
Hi,
See when i reading a sourcecode of a program,
I read that the constructor is ordinary and after that the programmer
has written virtual destructor for that constructor .
Why we use the virtual destructor whats the use of it?
the code is like this:

Network(int input,int output);
Network(&Network);
virtual ~Network();

My question is why he use virtual destructor.

1) Pointer to dynamically allocated class derived from Network can be
correctly deleted via a Network*
2) Network* can be used for dynamic_cast.

regards
Andy Little
 
K

kwikius

kwikius said:
1) Pointer to dynamically allocated class derived from Network can be
correctly deleted via a Network*

And as important correct destructor for derived from Newtwork will be
called, so any derived resources will be cleaned up.

regards
Andy Little
 
J

Jim Langston

sam said:
Hi,
See when i reading a sourcecode of a program,
I read that the constructor is ordinary and after that the programmer
has written virtual destructor for that constructor .
Why we use the virtual destructor whats the use of it?
the code is like this:

Network(int input,int output);
Network(&Network);
virtual ~Network();

My question is why he use virtual destructor.

If you derive from a base class and don't have a virtual destructor, there
are times when the derived classe's destructor won't be called.

Case in point, output from this program is:

Base1 Destructor
Derived2 Destructor
Base2 Destructor

We are missing a Derived2 Destructor

#include <iostream>
#include <string>

class Base1
{
public:
~Base1() { std::cout << "Base1 Destructor\n"; }
};

class Derived1: public Base1
{
public:
~Derived1() { std::cout << "Derived1 Destructor\n"; }
};

class Base2
{
public:
virtual ~Base2() { std::cout << "Base2 Destructor\n"; }
};

class Derived2 : public Base2
{
public:
~Derived2() { std::cout << "Derived2 Destructor\n"; }
};

int main ()
{
Base1* Foo = new Derived1;
Base2* Bar = new Derived2;

// delete Foo does not cause Derived1's destructor
// to be called, because Base1's destructor is not virtual
delete Foo;

delete Bar;

std::string wait;
std::getline( std::cin, wait );
}
 
H

hit_pc

Network(int input,int output);
Network(&Network);
virtual ~Network();

My question is why he use virtual destructor.
if :
class A:public Network{
//new some memory
}
 
J

Jim Langston

Jim Langston said:
If you derive from a base class and don't have a virtual destructor, there
are times when the derived classe's destructor won't be called.

Case in point, output from this program is:

Base1 Destructor
Derived2 Destructor
Base2 Destructor

We are missing a Derived2 Destructor

Correction, we are missing a Derived1 Destructor
 
P

Pete Becker

Jim said:
If you derive from a base class and don't have a virtual destructor, there
are times when the derived classe's destructor won't be called.

Case in point, output from this program is:

Base1 Destructor
Derived2 Destructor
Base2 Destructor

Only by accident, however. Formally, the behavior is undefined.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
E

E. Robert Tisdale

hit_pc said:
if :
class A:public Network{
//new some memory
}
-------
Network *network=new A();
delete network;//here will run the ~Network() not the ~A() if you didn't
add virtual before ~Network;
---------------------

That's just silly.

You should write:

A* pNetwork = new A();
delete pNetwork;


The virtual constructor implies a "virtual constructor"
(or better, a factory) so that you can write subprograms
which allow you to create and destroy copies of objects
passed by reference without knowing the actual type of the object.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top