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(&Networ k);
virtual ~Network();
My question is why he use virtual destructor. 7 2010
On Fri, 19 Jan 2007 20:46:20 +0800, sam <sa********@yah oo.comwrote:
Network(int input,int output);
Network(&Networ k);
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.
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(&Networ k);
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
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(&Networ k);
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
"sam" <sa********@yah oo.comwrote in message
news:11******** **************@ a75g2000cwd.goo glegroups.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(&Networ k);
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 );
}
"Jim Langston" <ta*******@rock etmail.comwrote in message
news:zf******** *******@newsfe0 6.lga...
"sam" <sa********@yah oo.comwrote in message
news:11******** **************@ a75g2000cwd.goo glegroups.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(&Netwo rk); 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 );
}
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)
hit_pc wrote:
sam wrote:
>Network(int input,int output); Network(&Netwo rk); 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 =---- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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" is called in Case 2 since only ~base() becomes
"virtual" and ~Derived() is still non-virtual?
3. Does Case 3 show that we don't need any virtual destructor to make
~Derived() called?
4. Is "virtual destructor" needed only for Case 2?
|
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, Interf:
class Interf
{
public:
|
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
destructors.
What I am expecting is, if the destructor is declared as virtual in
base, the destructors of all its derived classes also should be
virtual always.
What exactly is the reason for not having it so?
|
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 the case when the class contains at
least one virtual member function? Shouldn't the destructor always be
virtual, whenever there's a possibility that an inherited object will
be destructed through a base class pointer? (This does not require,
|
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 to pass all the necessary object files to the linker.
Neither of those are my problem. Please bear with me as the question
I ask is rather long and I think it's beyond a CS101 level of linker
stupidity. If it is a stupid CS101 mistake I'm making...
| |
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 item 7 for why this is virtual
...
|
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
|
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;
|
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;" instead? I think declaring a function as "=0" is the same
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |