473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1966
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 from Derived class? 2. Why "Derived destructor"...
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"? When is it needed to do so? For an interface,...
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 wondering, why the same concept was not used for the...
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 function." My question is: why is it only for...
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 for a prototype/header file item, or b) you forget...
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 { public: Person(); virtual ~Person(); // see...
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 = new A; B * b = new B; a = b;
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? Do I have to have "virtual void f() = 0;"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.