473,408 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

template inheritance

hi, all
Suppose I have a template class A

template<class Tclass A{...}

Then I have another class B which inherits class A. Should I say

class B: public A{...}

or class B: template<class TA{...}

or template<class Tclass B : public A {...}

or something else? I assume that class A is also a template. But can
class A be a non template class? If yes, how can A handle the different
type of T? Thanks ahead.

zl2k

Aug 17 '06 #1
4 9350

template<class T>
class Derived : public Base<T{

};

--

Frederick Gotham
Aug 17 '06 #2
zl2k schrieb:
hi, all
Suppose I have a template class A

template<class Tclass A{...}

Then I have another class B which inherits class A. Should I say

class B: public A{...}

or class B: template<class TA{...}

or template<class Tclass B : public A {...}
Depends.

If you want class B to be a template like class A, then:

template <class T>
class B : public A<T>
{
// ...
};

If you want that class B inherits from a specific class A, say with
template parameter int:

class B : public A<int>
{
// ...
};

--
Thomas
Aug 17 '06 #3

zl2k wrote:
hi, all
Suppose I have a template class A

template<class Tclass A{...}
No, that's a class template. A is not a class. A<intand A<charare
classes.
Then I have another class B which inherits class A. Should I say
Same problem, you can have a class B<intwhich inherits from A<int>
but
remember that A is not a class.
class B: public A{...}

or class B: template<class TA{...}

or template<class Tclass B : public A {...}

or something else? I assume that class A is also a template. But can
class A be a non template class? If yes, how can A handle the different
type of T? Thanks ahead.
template B<Tcan inherit from any class. When you instantiate it, you
have
a type for parameter T. Thus, you can also instantiate template A<Tas
a
base class, or A<std::pair<T,T, or std::ostream. The base class
doesn't
have to be a template instantiation. In fact, it often isn't. The base
class has
no more need to "handle" T as any member of B has. Only your design
will
tell you whether the base class needs to know about T, and how. (E.g.
it
might be a non-template class with a templated contructor)

Regards,
Michiel Salters

Aug 17 '06 #4

Mi*************@tomtom.com wrote:
zl2k wrote:
hi, all
Suppose I have a template class A

template<class Tclass A{...}

No, that's a class template. A is not a class. A<intand A<charare
classes.
Then I have another class B which inherits class A. Should I say

Same problem, you can have a class B<intwhich inherits from A<int>
but
remember that A is not a class.
class B: public A{...}

or class B: template<class TA{...}

or template<class Tclass B : public A {...}

or something else? I assume that class A is also a template. But can
class A be a non template class? If yes, how can A handle the different
type of T? Thanks ahead.

template B<Tcan inherit from any class. When you instantiate it, you
have
a type for parameter T. Thus, you can also instantiate template A<Tas
a
base class, or A<std::pair<T,T, or std::ostream. The base class
doesn't
have to be a template instantiation. In fact, it often isn't. The base
class has
no more need to "handle" T as any member of B has. Only your design
will
tell you whether the base class needs to know about T, and how. (E.g.
it
might be a non-template class with a templated contructor)

Regards,
Michiel Salters
Thanks for all the comments, very helpful. Have a good day.
zl2k

Aug 17 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Gandu | last post by:
Could some C++ guru please help me? I have a very odd problem with respect templates and inheritance. I have templatized List class, from which I am inheriting to create a Stack class. All works...
13
by: Walt Karas | last post by:
The following gives an error in the declaration of the member function x() of the class template Tpl, compiliing with a recent version of GCC under Solaris: class A { }; class B { }; ...
3
by: Thomas Matthews | last post by:
Hi, I would like to apply inheritance to a template parameter, but my design fails to compile: cannot initialize one template class with child child parameterized class. I'll explain... ...
2
by: Tony Johansson | last post by:
Hello Experts To derive a concrete class from a template class in invalid. Why?? So you should not be able have something like this class Derived : public Base<int, 100> It's valid to derive...
1
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. Im I right if I say the...
5
by: Tony Johansson | last post by:
Hello experts! I have two class template below with names Array and CheckedArray. The class template CheckedArray is derived from the class template Array which is the base class This program...
13
by: jois.de.vivre | last post by:
Hi All, I'm trying to write a wrapper class for std::vector to extend some of its functionality. The problem I'm getting into is returning an iterator type from a member function. Here is the...
2
by: Thomas Kowalski | last post by:
Hi, I would like to write a template class Polygon<VertexTypthere vertex typ can be eigther a pointer or a value typ. It has an attribute: std::vector<VertexTypvertices; And a methode:...
9
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. ...
32
by: Stephen Horne | last post by:
I've been using Visual C++ 2003 for some time, and recently started working on making my code compile in GCC and MinGW. I hit on lots of unexpected problems which boil down to the same template...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.