Thread: Templates and Inheritance

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    83

    Templates and Inheritance

    Hi,
    I'm trying to do something like the following...
    I would like fill a vector with objects that are of different type variations of the same template base class.
    The code below of course does not work because the vector needs a template arg. How do you get around this? A wrapper class? If so, how?
    Thanks!

    Code:
    template <class T>
    class MyClassBase {
    public:
    	MyClassBase() {
    	}
    	
    	T myValue;
    };
    
    class MyClassFloat : public MyClassBase<float> {
    	MyClassFloat(float myInput) {
    		myValue = myInput;
    	}
    };
    
    class MyClassInt : public MyClassBase<int> {
    	MyClassInt(int myInput) {
    		myValue = myInput;
    	}
    };
    
    int main (int argc, const char * argv[])
    {
        vector<MyClassBase> myVec;
        myVec.push_back(MyClassFloat(1.0));
        myVec.push_back(MyClassInt(1));
        return 0;
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Remove the template from the base class and add a template member function that is virtual, eg get, that you implement in the derived classes. Eg:

    Code:
    class Base { public: template<typename T> virtual T get() = 0; }
    class Derived1: public Base<Type1> { public: Derived1() { /*...*/ } template<typename T> virtual T get() { /* ... */ } };
    // And so on
    There is also a class named boost::any (boost libraries), or close to that, I believe. It might be of use.
    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
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    Hey, thanks, but that code doesn't work for me.
    I'm getting "virtual cannot be specified on member function templates."
    Any ideas?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    My fault, my fault. Didn't test it.
    Well, this calls for the classic "what are you trying to do?" question.
    There are many known design patterns, and one of them might just fit for what you want to do.
    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.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    I want to be able to store a vector of objects where each object is of the same base class (and can therefore be inserted into the vector), but where each object (of various derived classes) would return a different type for: getValue();

    so basically, I'd like to be able to do something like the following:
    vector<MyClass> myVector;
    myVector.push_back(MyClassIntVersion(1));
    myVector.push_back(MyClassFloatVersion(1.0));
    etc.

    Thanks!

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, but WHY do you want to do to that?
    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.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    Why? Because I want to be able to manage a group of objects that have similar properties but contain different types of data within one compact vector. Am I missing something?

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    Any thoughts? Thanks.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by pollypocket4eva View Post
    Why? Because I want to be able to manage a group of objects that have similar properties but contain different types of data within one compact vector. Am I missing something?
    Yes, what do you hope to accomplish by doing this?
    What is your ultimate goal? What are you trying to design?
    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
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    I'm creating an animation parameter system. An animation can have parameters of different types: float, int, etc. I'd like to store all of an object's parameters in a single vector or map and allow the object to access the value for each parameter as needed. Does that make sense?

  11. #11
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by pollypocket4eva View Post
    Hi,
    I'm trying to do something like the following...
    I would like fill a vector with objects that are of different type variations of the same template base class.
    The code below of course does not work because the vector needs a template arg. How do you get around this? A wrapper class? If so, how?
    Thanks!

    Code:
    template <class T>
    class MyClassBase {
    public:
    	MyClassBase() {
    	}
    	
    	T myValue;
    };
    
    class MyClassFloat : public MyClassBase<float> {
    	MyClassFloat(float myInput) {
    		myValue = myInput;
    	}
    };
    
    class MyClassInt : public MyClassBase<int> {
    	MyClassInt(int myInput) {
    		myValue = myInput;
    	}
    };
    
    int main (int argc, const char * argv[])
    {
        vector<MyClassBase> myVec;
        myVec.push_back(MyClassFloat(1.0));
        myVec.push_back(MyClassInt(1));
        return 0;
    }
    To get polymorphic behavior you're either going to have to allocate the desired derived types at runtime (with the aid of a "smart-pointer", hopefully) or else declare them on the stack and then store pointers to either (or maybe both) in your vector.

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by pollypocket4eva View Post
    I'm creating an animation parameter system. An animation can have parameters of different types: float, int, etc. I'd like to store all of an object's parameters in a single vector or map and allow the object to access the value for each parameter as needed. Does that make sense?
    Usually C++ is leveraged as for it's static type system. What you're doing is trying to undermine that. C++ is ok with that, but you got to ask yourself do I really need to do it?

    Assuming the answer is yes, the next question is to what degree do you want to enforce runtime type safety. What should happen if the user passes a float and treats it as an int? What mechanisms exist to prevent that? If they fail, do you want another failsafe? The simplest thing to do would be ignore type safety, but this could lead to program instability, especially if your types contain pointers.

    The most primitive way to store two types in an object should be obvious: you have an object holding each possible type it could hold. Does that solve the problem? If so, then C++ also provides a particular optimization: it can store those objects in the same memory location if you use a union instead of a class.

    But that won't provide any kind of type checking. If you want type checking built into the stored type, you have to do something more fancy. Boost::any provides such features, I believe. Or you can write it yourself; you would write accessor like getInt() or get<int>() that would only return correctly if the object was set with an int or whatever.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Difference between using Inheritance and Templates
    By Bargi in forum C++ Programming
    Replies: 1
    Last Post: 05-13-2010, 06:57 AM
  2. templates and inheritance
    By ichijoji in forum C++ Programming
    Replies: 2
    Last Post: 10-11-2004, 03:34 PM
  3. inheritance and templates
    By misplaced in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2004, 03:25 AM
  4. templates and inheritance problem
    By kuhnmi in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 02:46 AM
  5. Templates and Inheritance problem
    By WebSnozz in forum C++ Programming
    Replies: 0
    Last Post: 04-11-2004, 02:39 PM