472,337 Members | 1,444 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,337 software developers and data experts.

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 1856
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed...
39
by: Ele | last post by:
Is it correct to say that Whenever a class has a virtual member function, define its destructor as "virtual"? Can a destructor as "pure virtual"?...
7
by: qazmlp | last post by:
When a member function is declared as virtual in the base class, the derived class versions of it are always treated as virtual. I am just...
23
by: heted7 | last post by:
Hi, Most of the books on C++ say something like this: "A virtual destructor should be defined if the class contains at least one virtual member...
5
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code...
7
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person...
4
by: cwc5w | last post by:
I have two classes. One with a regular destructor and the other with a virtual destructor. e.g. class x { ~x(){} } vs
12
by: Avalon1178 | last post by:
Hello, Are default destructors virtual? In other words, say I have "class A" and "class B : public A", and I have the code below: A * a =...
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class?...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.