|
-
April 6th, 1999, 06:48 AM
#1
c++ question
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;
}
-
April 6th, 1999, 06:56 AM
#2
Re: c++ question
The destructors need to be virtual, as in:
Class A
{
public:
A();
virtual ~A();
};
class B : public A
{
public:
B();
private:
virtual ~B();
};
-
April 6th, 1999, 06:59 AM
#3
Re: c++ question
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.
-
April 6th, 1999, 07:16 AM
#4
Re: c++ question
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
-
April 6th, 1999, 07:28 AM
#5
Re: c++ question
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.
-
April 6th, 1999, 07:32 AM
#6
Re: c++ question
Hi,
The solution which you gave gives an error:
B::~B' : cannot access private member declared in class 'B'
Nitin
-
April 6th, 1999, 07:39 AM
#7
Re: c++ question
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
#8
Re: c++ question
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.
-
April 6th, 1999, 01:05 PM
#9
Re: c++ question
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.
-
April 6th, 1999, 02:38 PM
#10
Re: c++ question
I believe that Michael is correct.
For an interesting discussion of this topic,
see the June, 1998 C++ Q & A column in MSJ.
-
April 6th, 1999, 05:18 PM
#11
Re: c++ question
This is exactly correct. See Meyers, "Effective C++", Item 14.
-
April 7th, 1999, 01:21 PM
#12
Re: c++ question
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
#13
Re: c++ question
I guess I should say what the output of the code in the previous post was:
A::A()
B::B()
B::~B()
A::~A()
-
April 7th, 1999, 07:52 PM
#14
Re: c++ question
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
#15
Re: c++ question
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...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|