Click to See Complete Forum and Search --> : c++ question
Nitin
April 6th, 1999, 06:48 AM
The following code snippet works although my destructor of class B is declared private. Whereas, if I do not derive my class B virtual the following program fails to compile. Why? I am using VC++ 5.0.
class A {
public:
A() {
cout<<"\nIn constructor of A";
}
~A() {
cout<<"\nIn destructor of A";
}
};
class B : virtual public A {
public:
B() {
cout<<"\nIn Constructor of B";
}
private:
~B() {
cout<<"\nIn Destructor of B";
}
};
void main(void)
{
B b;
}
Michael Decker
April 6th, 1999, 06:56 AM
The destructors need to be virtual, as in:
Class A
{
public:
A();
virtual ~A();
};
class B : public A
{
public:
B();
private:
virtual ~B();
};
Michael Decker
April 6th, 1999, 06:59 AM
Also, if you declare the destructor as private, then no one can delete an object of class B. I believe even automatic variables as well as pointers.
Nitin
April 6th, 1999, 07:16 AM
Hi Michael,
Thankyou for a reply but what I am unable to understand right now is that what OOPS concept is making it happen and why would one need the destructors to be private and Why don't they work when they are not virtual.
Regards,
Nitin Sahjwani
Michael Decker
April 6th, 1999, 07:28 AM
A vast majority of destructors are NOT private. Where are you getting the notion that you need a private destructor?
When subclassing, the destructors are almost always virtual. This is to insure that all destructors in the chain get called.
Nitin
April 6th, 1999, 07:32 AM
Hi,
The solution which you gave gives an error:
B::~B' : cannot access private member declared in class 'B'
Nitin
Michael Decker
April 6th, 1999, 07:39 AM
Destructors a generally not private. If you make it public you should be OK.
I only made it private because that's what you originally had.
April 6th, 1999, 12:43 PM
If you make the destructor virtual, then all the destructors in the chain will NOT get called. It's the other way around, virtual says not to call the original one, non-virtual says to call all of them.
Michael Decker
April 6th, 1999, 01:05 PM
When it comes to the destructor, I disagree.
Virtual destructors provide the means for each of the classes in the chain to clean themselves up in the reverse order that they were created. By not making them virtual, you run the risk of memory leaks because things weren't destroyed correctly.
When you have other member functions that are virtual, then you're right you have to explicitly call the base class' function if that's what you want.
Gomez Addams
April 6th, 1999, 02:38 PM
I believe that Michael is correct.
For an interesting discussion of this topic,
see the June, 1998 C++ Q & A column in MSJ.
LALeonard
April 6th, 1999, 05:18 PM
This is exactly correct. See Meyers, "Effective C++", Item 14.
April 7th, 1999, 01:21 PM
I had to test this to make sure and now I'm confused :(
Here's the code I tried:
class A
{
public:
A();
virtual ~A();
};
class B : public A
{
public:
B();
virtual ~B();
};
A::A()
{
printf("A::A()\r\n");
}
A::~A()
{
printf("A::~A()\r\n");
}
B::B()
{
printf("B::B()\r\n");
}
B::~B()
{
printf("B::~B()\r\n");
}
int main(int argc, char* argv[], char* envp[])
{
B b;
return 0;
}
And if I declare the destructor as virtual, it behaves the same way as if I didn't. I have a feeling I missed something in the conversation here. Somebody wanna clear it up a bit? Thanks.
April 7th, 1999, 01:22 PM
I guess I should say what the output of the code in the previous post was:
A::A()
B::B()
B::~B()
A::~A()
Michael Decker
April 7th, 1999, 07:52 PM
That's the correct output for the example you're showing.
Try it using pointers and dynamically creating the object using 'new'. That's generally how things are done anyway when you want polymorphism.
For example:
A *pObject = new B;
delete pObject;
Without virtual destructors, you might run into trouble with the above example.
Let me know how you make out.
Thanks,
Michael
April 8th, 1999, 11:51 AM
Hi Michael,
I guess I wasn't quite thinking :( If you delete an object using a pointer to the base class, you need virtual destructors to ensure that all of the destructors (even in derived class) get called....
It all makes sense now :)
Thanks...
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.