problem with classes and pointers

This is a discussion on problem with classes and pointers within the C++ Programming forums, part of the General Programming Boards category; I believe the rule is that if you have a virtual function, the destructor should be virtual since it implies ...

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    I believe the rule is that if you have a virtual function, the destructor should be virtual since it implies you are going to use polymorphism. Inheritance does not necessarily mean you are going to use polymorphism, thus virtual destructors are usually only an overhead.
    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.

  2. #17
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Well, I dunno exactly what virtual means, but it doesn't change anything serious, if placed wrong, right?
    Currently research OpenGL

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    Best read up about inheritance, polymorphism and virtual functions, as well as abstract base classes (pure virtual functions).
    Not using virtual correct can mean overhead and headaches.

    Also, while deriving classes will "inherit" the virtual status of functions (including destructors), it is good practice, methinks, to actually mark them as virtual anyway, because it may not be obvious they are virtual unless you take a look at the base class and it doesn't hurt to put virtual in there anyway.
    Last edited by Elysia; 02-21-2009 at 06:26 AM.
    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.

  4. #19
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,415
    Yes, it will.

    Despite what has been offered, you only need a virtual destructor if you are going to destroy an object polymorphically. (If you religiously use good smart pointers, it is really a non-issue, but still, rather safe than sorry.) However, if you get this wrong, you destroy the universe. Consequently, if you already have one virtual method, marking the destructor as virtual will save you a headache and probably cost you only a little overhead--probably only measurable in hundreds of thousands of iterations or tens of bytes.

    By the by, it is always the "handle" class that needs a virtual destructor. (Where the "handle" class is whatever type you will store a pointer to for the sake of polymorphic destruction.) The destructor for classes that inherit from the "handle" class do not need to be marked 'virtual'.

    Soma
    Last edited by phantomotap; 02-21-2009 at 06:25 AM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Q: Pointers to Classes '->' notation
    By Howie17 in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2008, 10:09 AM
  2. Replies: 12
    Last Post: 12-31-2007, 06:59 AM
  3. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 07:18 AM
  4. Pointers to Classes || pointers to structures
    By C++Child in forum C++ Programming
    Replies: 24
    Last Post: 07-30-2004, 07:14 PM
  5. Help With pointers in classes.
    By indigo0086 in forum C++ Programming
    Replies: 12
    Last Post: 10-10-2002, 03:03 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21