By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,117 Members | 1,093 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,117 IT Pros & Developers. It's quick & easy.

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

P: 31
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
Apr 24 '18 #1

✓ answered by weaknessforcats

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.

Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
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.
Apr 24 '18 #2

P: 31
Ah ooops :) Forgot that was different with pointers
Apr 24 '18 #3

P: 31
Thanks! I'll try it again and remove the functions, and if there are still problems, I try it with some casting
Apr 24 '18 #4

Post your reply

Sign in to post your reply or Sign up for a free account.