Skip to main content
GameDev.net gamedev.net
🔒 Locked

Templated Base Classes

Started by CpMan Oct 7, 2002 at 12:16 AM 2 replies 200+ views
Original Post
CpMan
CpMan
Is the template portion of a base class in C++ inherited in a child class?
VSEDebug Visual Studio.NET Add-In. Enhances debugging in ways never thought possible.
Oluseyi
Oluseyi
Yes, but not necessarily in the same form:

template < typename T >
BaseClass
{
protected:
T * t_ptr;
T t_instance;
};
...
// note that DerivedClass is not necessarily a template
class DerivedClass : public BaseClass< int >
{
// class inherits t_ptr as int * and t_instance as int
};

Obviously, if the type used to instantiate BaseClass is a concrete type for which no default constructor exists, DerivedClass will fail to compile.
Fruny
Fruny
Let me rephrase that

A class can only inherit from a fully instanciated template.

Whether to derive from a ''fixed'' specialisation
- class Derived : public Parent<int, double> {};

or to leave that control to a template parameter of the derived class

- template <class T, class U> class Derived : public Parent<T, U> {};

or to mix and match

- template <class T> class Derived : public Parent<T, double> {}; *

is entirely up to you.

* This is not partial template specialisation.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
CpMan
CpMan
Ahh I see, thanks.


Reality is Relative -Me and Probably somebody else
VSEDebug Visual Studio.NET Add-In. Enhances debugging in ways never thought possible.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.