Thread: Doubt regarding destructors

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    1

    Doubt regarding destructors

    I have the following class hierarchy:

    Shape <- Shape3d <- Sphere

    And I am trying to run the following code:

    Code:
    Shape *s = (Shape3d *) new Sphere();
    ...
    delete s;
    I have destructors in place with cout statements. I can see that only Shape destructor is called. How do I ensure that Sphere, Shape3d and Shape destructors are called?

    I am using Turbo C++ IDE.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Make the desctructors virtual.
    Furthermore, do not cast the return of new! This is complete unnecessary.
    Also recommend upgrading your IDE if you can. Turbo C++ is so outdated and non-standard.
    Visual Studio (Express) is a very good IDE with a compiler and debugger.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    Furthermore, do not cast the return of new! This is complete unnecessary.
    And even if you do need to use casts elsewhere in the code, don't use C-style casts. Use the proper C++ casts: static_cast, dynamic_cast, const_cast or reinterpret_cast.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Ooh, I found the problem!
    Quote Originally Posted by karishma View Post
    I am using Turbo C++ IDE.
    You probably don't want to do that!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  2. Doubt abt Storage!
    By kalamram in forum C Programming
    Replies: 1
    Last Post: 04-21-2006, 05:30 AM
  3. Destructors in dynamically allocated arrays
    By frenchfry164 in forum C++ Programming
    Replies: 1
    Last Post: 11-28-2003, 11:26 PM
  4. Virtual & Pure virtual destructors
    By BMJ in forum C++ Programming
    Replies: 61
    Last Post: 08-22-2002, 09:38 AM
  5. Greatest C++ Doubt
    By vasanth in forum C++ Programming
    Replies: 15
    Last Post: 02-28-2002, 04:41 AM