![]() |
~CXTPControlCustom() should be virtual! |
Post Reply
|
| Author | ||||||||||||||||||||||||||||
znakeeye
Senior Member
Joined: 26 July 2006 Status: Offline Points: 1672 |
Post Options
Thanks(0)
Quote Reply
Topic: ~CXTPControlCustom() should be virtual!Posted: 10 April 2009 at 2:35pm |
|||||||||||||||||||||||||||
|
Quite important actually.
|
||||||||||||||||||||||||||||
|
PokerMemento - http://www.pokermemento.com/
|
||||||||||||||||||||||||||||
![]() |
||||||||||||||||||||||||||||
Oleg
Senior Member
Joined: 21 May 2003 Location: United States Status: Offline Points: 11234 |
Post Options
Thanks(0)
Quote Reply
Posted: 10 April 2009 at 3:59pm |
|||||||||||||||||||||||||||
|
Hi,
If there is no "virtual" keyword it doesn't mean its not virtual ;-)
ps. Don't worry, its virtual.
|
||||||||||||||||||||||||||||
|
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS |
||||||||||||||||||||||||||||
![]() |
||||||||||||||||||||||||||||
znakeeye
Senior Member
Joined: 26 July 2006 Status: Offline Points: 1672 |
Post Options
Thanks(0)
Quote Reply
Posted: 13 April 2009 at 2:55pm |
|||||||||||||||||||||||||||
|
That depends on how you use it. The code is dangerous. Try this sample:
struct A
{
A() {}
~A() {}
};
struct B : public A
{
B() {}
virtual ~B() {} // Correctly written, derived class.
};
A *a = new B;
delete a; <--- crash!
Trust me, the "virtual" keyword is extremely important! |
||||||||||||||||||||||||||||
|
PokerMemento - http://www.pokermemento.com/
|
||||||||||||||||||||||||||||
![]() |
||||||||||||||||||||||||||||
Oleg
Senior Member
Joined: 21 May 2003 Location: United States Status: Offline Points: 11234 |
Post Options
Thanks(0)
Quote Reply
Posted: 14 April 2009 at 12:43am |
|||||||||||||||||||||||||||
|
Try this sample
struct A
{
A() {}
virtual ~A() {}
};
struct B : public A
{
B() {}
~B() {} // destructor B will be virtual even without virtual keyword
};
A *a = new B;
delete a;
Trust me, I now what is "virtual" keyword and how it used :)
p.s. See 10.3.1 of ANSI C++ International Standard |
||||||||||||||||||||||||||||
|
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS |
||||||||||||||||||||||||||||
![]() |
||||||||||||||||||||||||||||
znakeeye
Senior Member
Joined: 26 July 2006 Status: Offline Points: 1672 |
Post Options
Thanks(0)
Quote Reply
Posted: 14 April 2009 at 3:46am |
|||||||||||||||||||||||||||
|
Yes, of course. But...
struct C : public B
{
};
B *b = new C;
delete b; // leak or crash
Public destructors should always be declared virtual, period.
![]() |
||||||||||||||||||||||||||||
|
PokerMemento - http://www.pokermemento.com/
|
||||||||||||||||||||||||||||
![]() |
||||||||||||||||||||||||||||
Oleg
Senior Member
Joined: 21 May 2003 Location: United States Status: Offline Points: 11234 |
Post Options
Thanks(0)
Quote Reply
Posted: 14 April 2009 at 8:33am |
|||||||||||||||||||||||||||
|
Hi,
So you want to compete in C++ knowledge ? :)
www.brainbench.com -> C++ Fundametals.
try to score more :)
ps. I bet 100$ that you didn't try last sample.
Try this.
struct A
{ A() {} virtual ~A() {printf("Called ~A\n");} }; struct B : public A
{ B() {} ~B() { printf("Called ~B\n");} // destructor B will be virtual even without virtual keyword }; struct C : public B
{ ~C() { printf("Called ~C\n");} // destructor B will be virtual even without virtual keyword }; int main(int argc, char* argv[])
{ B *b = new C;
delete b; // no leak. no crash. return 0;
} |
||||||||||||||||||||||||||||
|
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS |
||||||||||||||||||||||||||||
![]() |
||||||||||||||||||||||||||||
ABuenger
Newbie
Joined: 02 February 2006 Status: Offline Points: 1075 |
Post Options
Thanks(0)
Quote Reply
Posted: 14 April 2009 at 9:01am |
|||||||||||||||||||||||||||
|
Virtual **** comparison!!!
![]() |
||||||||||||||||||||||||||||
|
Codejock support
|
||||||||||||||||||||||||||||
![]() |
||||||||||||||||||||||||||||
znakeeye
Senior Member
Joined: 26 July 2006 Status: Offline Points: 1672 |
Post Options
Thanks(0)
Quote Reply
Posted: 14 April 2009 at 10:49am |
|||||||||||||||||||||||||||
|
Ok, so the destructor in A saves you in this case, but that doesn't motivate leaving out the "virtual" keyword. I ain't gonna compete with my master (though Anakin will become stronger one day).
However...
![]() International Standard ISO/IEC 14882: http://www.gisinvestor.ru/?module=File&action=Download&id=8
If the object being deleted has incomplete class type at the point of deletion and the complete class has a nontrivial destructor or a deallocation function the behavior is undefined.
Q: Why would you NOT explicitly make the destructor virtual? If CXTPControlCustom was some day changed to inherit some other class there is a high risk the class causes problems when derived!
My answer: Public destructors should always be explicitly declared virtual. Just like you check pointers before using them, this is a way to avoid problems.
|
||||||||||||||||||||||||||||
|
PokerMemento - http://www.pokermemento.com/
|
||||||||||||||||||||||||||||
![]() |
||||||||||||||||||||||||||||
Oleg
Senior Member
Joined: 21 May 2003 Location: United States Status: Offline Points: 11234 |
Post Options
Thanks(0)
Quote Reply
Posted: 14 April 2009 at 12:48pm |
|||||||||||||||||||||||||||
|
Hi :)
See 10.3.2 from this standard.
ok, think we find out that ~CXTPControlCustom is virtual even without virtual keword. so discussion can be closed.
|
||||||||||||||||||||||||||||
|
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS |
||||||||||||||||||||||||||||
![]() |
||||||||||||||||||||||||||||
Post Reply
|
|
|
Tweet
|
| Forum Jump | Forum Permissions ![]() You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum |