Templates and inheritance
Here's a templated parent class:
Code:
template<class T>
class A
{
public:
A(T param);
~A();
}
template<class T>
A::A(T param)
{
// do stuff with type T parameter
}
template<class T>
A::~A()
{
// undo stuff
}
I want to inherit this class with a specific type, like this:
Code:
class B : public A<int> // or any other type
{
public:
B(int param);
~B();
}
B::B(int param) : A(param)
{
}
B::~B()
{
}
Is this possible? Is this the right syntax to do it?
Re: Templates and inheritance
Re: Templates and inheritance
It is definitely possible. Syntax looks okay. I usually typedef template instantiations.
Code:
typedef A<int> AInstantiation;
class B : public AInstantiation
Not sure if it makes a difference.
Also, from my limited experience templates and inheritance are exclusive (not in the standard but in the practice). Do you really want inheritance?
Re: Templates and inheritance
It was common in the first years of templates to derive a template from a 'standard' class, but the reverse was discouraged from worries of code bloat. The real issue back then was that 64 MBytes of RAM was about $1,000, and compilers would choke on template code.
In modern work you will find template bases to templates, which themselves are part of libraries you may be expected to derive "standard" classes from, very much like you're doing here.
It can be a means of introducing traits or other compile time modulations of the base behavior. You can think of the base template as an interface, where the type supplied to it may be an implementation. There are many other uses.
You may be ready to dive into Alexandrescu's (sp?) book on "Modern C++ Design - Generic Programming and Design Patterns Applied", and/or any of the "Exceptional C++" series, as they discuss these notions.
Quote:
Do you really want inheritance?
It's certainly a valid question.
The reason for construction of this type may be that the base represents part of a library you're creating, and the type supplies a trait or behavior selection at compile time, which traditionally may have been part of some parameter provided during base class construction.
In a variation on this idea, you'll find that string is a typedef for a class which accepts traits and is derived from a template class in much the same way you provide here. The purpose is to allow string to operate as a 'common' ASCII type string, or a unicode "wide character" string by selecting the trait.
The issue comes to mind about the requirements of doing this. The base class will be a type relative to the parameter (int in your example), which means you're not going to be 'storing' these things in a container based on that expression - you want that to remain anonymous, which hints that the inheritance might not be public. Do you want to have to provide a virtual destructor? Do you need inheritance from the template, or is the standard class a "consumer" of this template? (the classic Is-a or Has-a question).
Re: Templates and inheritance
Quote:
The reason for construction of this type may be that the base represents part of a library you're creating, and the type supplies a trait or behavior selection at compile time, which traditionally may have been part of some parameter provided during base class construction.
In a variation on this idea, you'll find that string is a typedef for a class which accepts traits and is derived from a template class in much the same way you provide here. The purpose is to allow string to operate as a 'common' ASCII type string, or a unicode "wide character" string by selecting the trait.
Few months ago, i have developed a Graph template that accept traits at CT which can operate on directed or undirected graph.
The behaviour selection is policy class.
Not sure how string was implemented.
Re: Templates and inheritance
Code:
class B : public A<int>
I've used this technique a lot in my GUI & image library to create customised concrete types from a generic interface template.
It can also be an alternative method of getting around multiple inheritance problems, where you supply the class to derive from as the template parameter.
Code:
tempate <typename T>
class Window : public T
{
// Common helper functions.
};
class Special_Edit_Box : public Window<CEdit>
{
// Edit box functions.
};