Print Page | Close Window

~CXTPControlCustom() should be virtual!

Printed From: Codejock Forums
Category: Codejock Products
Forum Name: Toolkit Pro
Forum Description: Topics Related to Codejock Toolkit Pro
URL: http://forum.codejock.com/forum_posts.asp?TID=13978
Printed Date: 22 June 2025 at 6:41pm
Software Version: Web Wiz Forums 12.04 - http://www.webwizforums.com


Topic: ~CXTPControlCustom() should be virtual!
Posted By: znakeeye
Subject: ~CXTPControlCustom() should be virtual!
Date Posted: 10 April 2009 at 2:35pm
Quite important actually.

-------------
PokerMemento - http://www.pokermemento.com/



Replies:
Posted By: Oleg
Date 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


Posted By: znakeeye
Date 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/


Posted By: Oleg
Date 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


Posted By: znakeeye
Date 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/


Posted By: Oleg
Date Posted: 14 April 2009 at 8:33am
Hi,
 
So you want to compete in C++ knowledge ? :)
 
  http://www.brainbench.com - www.brainbench.com  -> C++ Fundametals.
 

Test:

C++ Fundamentals

Date:

10-Feb-2009

Score:

4.76

Weights:

100% C++ Fundamentals

Elapsed time:

49 min 19 sec

C++ Fundamentals

Score:

4.76

Percentile:

Scored higher than 97% of previous examinees

Demonstrates understanding of most advanced concepts within the subject area. Appears capable of mentoring others on the most complex projects.

Strong Areas

  • Data Handling
  • Flow Control
  • Functions
  • Building C++ Programs
  • Templates

Weak Areas

  • None noted
 
 
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


Posted By: ABuenger
Date Posted: 14 April 2009 at 9:01am
Virtual **** comparison!!! 



-------------
Codejock support


Posted By: znakeeye
Date 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 - 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/


Posted By: Oleg
Date 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



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.04 - http://www.webwizforums.com
Copyright ©2001-2021 Web Wiz Ltd. - https://www.webwiz.net