Codejock Forums Homepage
Forum Home Forum Home > Codejock Products > Visual C++ MFC > Toolkit Pro
  New Posts New Posts RSS Feed - ~CXTPControlCustom() should be virtual!
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

~CXTPControlCustom() should be virtual!

 Post Reply Post Reply
Author
Message
znakeeye View Drop Down
Senior Member
Senior Member
Avatar

Joined: 26 July 2006
Status: Offline
Points: 1672
Post Options Post Options   Thanks (0) Thanks(0)   Quote znakeeye Quote  Post ReplyReply Direct Link To This Post Topic: ~CXTPControlCustom() should be virtual!
    Posted: 10 April 2009 at 2:35pm
Quite important actually.
PokerMemento - http://www.pokermemento.com/
Back to Top
Oleg View Drop Down
Admin Group
Admin Group


Joined: 21 May 2003
Location: United States
Status: Offline
Points: 11234
Post Options Post Options   Thanks (0) Thanks(0)   Quote Oleg Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
znakeeye View Drop Down
Senior Member
Senior Member
Avatar

Joined: 26 July 2006
Status: Offline
Points: 1672
Post Options Post Options   Thanks (0) Thanks(0)   Quote znakeeye Quote  Post ReplyReply Direct Link To This Post 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/
Back to Top
Oleg View Drop Down
Admin Group
Admin Group


Joined: 21 May 2003
Location: United States
Status: Offline
Points: 11234
Post Options Post Options   Thanks (0) Thanks(0)   Quote Oleg Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
znakeeye View Drop Down
Senior Member
Senior Member
Avatar

Joined: 26 July 2006
Status: Offline
Points: 1672
Post Options Post Options   Thanks (0) Thanks(0)   Quote znakeeye Quote  Post ReplyReply Direct Link To This Post 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/
Back to Top
Oleg View Drop Down
Admin Group
Admin Group


Joined: 21 May 2003
Location: United States
Status: Offline
Points: 11234
Post Options Post Options   Thanks (0) Thanks(0)   Quote Oleg Quote  Post ReplyReply Direct Link To This Post Posted: 14 April 2009 at 8:33am
Hi,
 
So you want to compete in C++ knowledge ? :)
 
 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
Back to Top
ABuenger View Drop Down
Newbie
Newbie
Avatar

Joined: 02 February 2006
Status: Offline
Points: 1075
Post Options Post Options   Thanks (0) Thanks(0)   Quote ABuenger Quote  Post ReplyReply Direct Link To This Post Posted: 14 April 2009 at 9:01am
Virtual **** comparison!!! 

Codejock support
Back to Top
znakeeye View Drop Down
Senior Member
Senior Member
Avatar

Joined: 26 July 2006
Status: Offline
Points: 1672
Post Options Post Options   Thanks (0) Thanks(0)   Quote znakeeye Quote  Post ReplyReply Direct Link To This Post 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...
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/
Back to Top
Oleg View Drop Down
Admin Group
Admin Group


Joined: 21 May 2003
Location: United States
Status: Offline
Points: 11234
Post Options Post Options   Thanks (0) Thanks(0)   Quote Oleg Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.04
Copyright ©2001-2021 Web Wiz Ltd.

This page was generated in 0.141 seconds.