C++ inheriting template class from non-template base class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 51423benam
    New Member
    • Apr 2018
    • 31

    C++ inheriting template class from non-template base class

    Hello,

    I have a template class, which uses a class argument as a specifier of member types. It's basically a specific generic container, which has function return datatypes and variable datatypes depending on the template argument.
    Now, I want to store a couple of these containers in a vector, but I want to be able to put instances of different template argument types in there. So I want to be able to put in a container of float, a container of int, whatever (by the way, you may wonder "why is he reinventing the wheel?", but these have to have specific functions and they're fit in to my design).
    The problem is, although it's possible to let a template class inherit from a not-template base class (what would be necessary for storing in a vector), the base class has to have all functions already with fixed return datatypes.
    But I need them to change depending on the template argument. Is that possible? Can I maybe somehow pass the argument to the base class?

    Thanks
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    At run time there are no templates, no classes, no data types. All there is only binary code.

    So the type changes must be made at compile time and not by the compiler but by the preprocessor. By the time the compiler gets the code all substitutions have been made. At this point the code is called a translation object.

    Does this fit with your design?

    BTW the base class does not need all functions in the hierarchy. The advantage of deriving is to be able to add functionality to a derived class. Most times the derived object is accessed through a base class pointer that contains the address of the derived object. The base class needs only those functions common to the hierarchy. A virtual function may be required to signal the derived class must provide code for the method when the derived class us used through a base class pointer.

    Comment

    • 51423benam
      New Member
      • Apr 2018
      • 31

      #3
      Ah ooops :) Forgot that was different with pointers

      Comment

      • 51423benam
        New Member
        • Apr 2018
        • 31

        #4
        Thanks! I'll try it again and remove the functions, and if there are still problems, I try it with some casting

        Comment

        Working...