How properly inherit from template?

This is a discussion on How properly inherit from template? within the C++ Programming forums, part of the General Programming Boards category; Is this correct or are the functions not properly overriding? Code: template < typename T> class Test { T* get() ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    How properly inherit from template?

    Is this correct or are the functions not properly overriding?

    Code:
    template <typename T>
    class Test
    {
         T* get();
         void destroy();
    };
    
    template <typename T> T* Test<T>::get()
    {
     ....
    }
    
    template <typename T> void Test<T>::destroy()
    {
     ....
    }
    
    //Now the class that inherits
    class SomeClassTest : public Test<SomeClass>
    {
         SomeClass* get();
         void destroy();
    };
    
    SomeClass* SomeClassTest::get()
    {
         ...elided...
         return ( SomeClass* ) Test<SomeClass>::get();
    }
    
    void SomeClassTest::destroy() 
    {
         ...elided...
    }
    Last edited by 6tr6tr; 04-22-2008 at 02:42 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    There are no virtual functions to override.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by laserlight View Post
    There are no virtual functions to override.
    Thanks, so if I mark those functions as virtual then is the rest of the code properly overriding it?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    Thanks, so if I mark those functions as virtual then is the rest of the code properly overriding it?
    Maybe, since "properly" depends on whether it does what it is supposed to do. Note that Test should have a virtual destructor, and the member functions should be public, not private.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    Quote Originally Posted by laserlight View Post
    Note that Test should have a virtual destructor, and the member functions should be public, not private.
    They always say that when you use virtual functions. But in truth, it's not required, though it is often a safe thing to do to make sure objects destruct properly.
    I don't know if I would say that it's required to add a virtual destructor, but more like recommended.

    Having virtual functions does not automatically imply that you are going to delete a class via a base class pointer.
    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.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    I don't know if I would say that it's required to add a virtual destructor, but more like recommended.
    I wrote should, not must.

    Having virtual functions does not automatically imply that you are going to delete a class via a base class pointer.
    It does imply that the class is designed to be a polymorphic base class. So you have a choice: either warn your users not to subclass your class and then delete objects of their derived class via a pointer to your base class, or you simply make your base class destructor virtual. Which is easier and less error prone?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    Quote Originally Posted by laserlight View Post
    I wrote should, not must.
    But in my view of sense, that almost means the same as must. Should or must, they both imply that if the class has virtual functions it needs a virtual destructor. Perhaps should is too strong a word?

    It does imply that the class is designed to be a polymorphic base class. So you have a choice: either warn your users not to subclass your class and then delete objects of their derived class via a pointer to your base class, or you simply make your base class destructor virtual. Which is easier and less error prone?
    I agree, it is less error prone to make it virtual, which is also typically why it's a golden rule, but what I wondered mostly was if it should be a "should" or "must" and not "recommended" or "strongly recommended".
    It sounded like it's a rule you need to follow.
    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.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    But in my view of sense, that almost means the same as must. Should or must, they both imply that if the class has virtual functions it needs a virtual destructor. Perhaps should is too strong a word?
    "Must" is stronger than "should". Stroustrup states in his answer to the FAQ Why are destructors not virtual by default?: "So when should I declare a destructor virtual? Whenever the class has at least one virtual function."

    It sounded like it's a rule you need to follow.
    It is a corollary of the rule "do not publicly inherit from standard containers". It is not a rule specified by the standard, but one specified by best practice.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    Quote Originally Posted by laserlight View Post
    "Must" is stronger than "should". Stroustrup states in his answer to the FAQ Why are destructors not virtual by default?: "So when should I declare a destructor virtual? Whenever the class has at least one virtual function."
    Yes, it's good practice because it's less error prone.

    It is a corollary of the rule "do not publicly inherit from standard containers". It is not a rule specified by the standard, but one specified by best practice.
    However, seeing as it is merely good practice, and not required by the standard, I wouldn't go as far as to say "should," but merely recommended or good practice, because there may be instances where you might not want virtual destructors.

    I am known to disagree with Bjornstrop and other C++ experts at times
    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.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    However, seeing as it is merely good practice, and not required by the standard, I wouldn't go as far as to say "should," but merely recommended or good practice, because there may be instances where you might not want virtual destructors.
    Indeed, you would not want a virtual destructor if the class is not a polymorphic base class. If the class is not a polymorphic base class, why would you want virtual functions?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    If the standard requires something, it's "must". If it's something that is important even though not required, it's "should", like virtual destructors in base classes and the rule of three.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    I still say recommended, because should implies that it should always be done, which may not be the case. There are cases (speed concerns) where virtual destructors may not be used. There I propose the use of "recommended" and not "should."
    But each to their own.
    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.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    There are cases (speed concerns) where virtual destructors may not be used.
    Of all the reasons for not using virtual destructors, speed concerns really shouldn't be there.
    To have a virtual destructor called virtually, you need to make a heap deallocation. If the object was on the stack, the real type of the object would be known and the destructor call would be non-virtual. Deallocating something on the heap is several orders of magnitude more expensive than a virtual call, so, no. Not a good argument.
    The main reason not to use virtual destructors is to not introduce a vtable where it isn't needed. However, if any other members are virtual, you already have a vtable.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    I was mostly stabbing at a theory, though.
    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.

  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by Elysia View Post
    I was mostly stabbing at a theory, though.
    Yes, but for someone who decries certain functions in the standard library as "evil", I'm surprised to see such a cavalier attitude towards what is a highly recommended practice by the most respected members of the C++ community (Sutter, Meyers, Stroustrup, etc.).

Page 1 of 8 12345678 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

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