469,623 Members | 1,831 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

About virtual destructor

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.

Jan 19 '07 #1
7 1706
On Fri, 19 Jan 2007 20:46:20 +0800, sam <sa********@yahoo.comwrote:
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
}
-------
Network *network=new A();
delete network;//here will run the ~Network() not the ~A() if you didn't
add virtual before ~Network;
---------------------

--
Hello,World!
----legolaskiss.
Jan 19 '07 #2
sam wrote:
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

Jan 19 '07 #3

kwikius wrote:
sam wrote:
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*
And as important correct destructor for derived from Newtwork will be
called, so any derived resources will be cleaned up.

regards
Andy Little

Jan 19 '07 #4
"sam" <sa********@yahoo.comwrote in message
news:11**********************@a75g2000cwd.googlegr oups.com...
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 );
}
Jan 19 '07 #5

"Jim Langston" <ta*******@rocketmail.comwrote in message
news:zf***************@newsfe06.lga...
"sam" <sa********@yahoo.comwrote in message
news:11**********************@a75g2000cwd.googlegr oups.com...
>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
Correction, we are missing a Derived1 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 );
}


Jan 19 '07 #6
Jim Langston wrote:
>
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)
Jan 19 '07 #7
hit_pc wrote:
sam wrote:
>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
}
-------
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.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jan 20 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Stub | last post: by
39 posts views Thread by Ele | last post: by
4 posts views Thread by cwc5w | last post: by
12 posts views Thread by Avalon1178 | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.