Thread: How properly inherit from template?

  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
    28,412
    There are no virtual functions to override.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,412
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    28,412
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    28,412
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    28,412
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,895
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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,895
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.).

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, 12:54 PM