Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Inheriting template class

Reply
Thread Tools

Inheriting template class

 
 
jarradw@gmail.com
Guest
Posts: n/a
 
      05-09-2006
I am trying to figure out if this will work. I have a template base
class that I want to be able to inherit from, the classes that will
need to inherit from this also need to be tamplates. Is this possible
or do i need to think of another way to do it?

 
Reply With Quote
 
 
 
 
Noah Roberts
Guest
Posts: n/a
 
      05-09-2006

jarr...@gmail.com wrote:
> I am trying to figure out if this will work. I have a template base
> class that I want to be able to inherit from, the classes that will
> need to inherit from this also need to be tamplates. Is this possible
> or do i need to think of another way to do it?


template<typename X>
class C1
{
};

template<typename Y>
class C2 : public C1<Y>
{
Y var;
};

is but one way of doing it, meeting a particular set of needs.

 
Reply With Quote
 
 
 
 
jarradw@gmail.com
Guest
Posts: n/a
 
      05-09-2006
Hmm I tried that and I get an error saying that C2 is not a template
name

 
Reply With Quote
 
Jarrad
Guest
Posts: n/a
 
      05-09-2006
correction not a template type

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      05-09-2006
wrote:
> I am trying to figure out if this will work. I have a template base
> class


Actually, it sounds like you have a class template you want to use as
a base for something.

> that I want to be able to inherit from, the classes that will
> need to inherit from this also need to be tamplates. Is this possible
> or do i need to think of another way to do it?


You need to get one thing straight: you can inherit only from a class.
You cannot inherit from a template. Even when you define another class
template like so:

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

at the time of instantiating the 'Derived' class with a particular 'T',
say, 'Blah', it will *inherit* from a "normal" *class* (in that case
'Base<Blah>'). IOW, no inheriting from templates exists in C++. In
order to make 'Derived' a normal class (and not a template), you _have_
to derive it from an instantiation of the 'Base' template, like so

class SomeDerived : public Base<Foo> { ...

Please ask more specific questions if something's still unclear.

V
--
Please remove capital As from my address when replying by mail


 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      05-09-2006

wrote:
> Hmm I tried that and I get an error saying that C2 is not a template
> name


You'll need to provide more information and quote better in order for
me to be able to help you.

 
Reply With Quote
 
Jarrad
Guest
Posts: n/a
 
      05-09-2006
template <class T>
class C1{
virtual void output();
}

template <class T>
class C2: public C1<T>
{
virtual void output();
}

I have other functions and such in the classes but that is the basic
setup of how I am trying to inherit, but as Victor said I may not be
able to inherit this way.

 
Reply With Quote
 
Jarrad
Guest
Posts: n/a
 
      05-09-2006
Ok so I took out all the inheritance parts and now have two template
classes
template <class T>
class C1
{
virtual void output();

}

template <class T>
class C2
{
virtual void output();

}

How ever I am still getting the error about C2 not being a tamplate
type. I am starting to think that my error may be somewhere else.

 
Reply With Quote
 
Jarrad
Guest
Posts: n/a
 
      05-09-2006
Ok so I took out all the inheritance parts and now have two template
classes
template <class T>
class C1
{
void output();

}

template <class T>
class C2
{
void output();

}

How ever I am still getting the error about C2 not being a tamplate
type. I am starting to think that my error may be somewhere else.

 
Reply With Quote
 
W Marsh
Guest
Posts: n/a
 
      05-09-2006
On 9 May 2006 16:08:35 -0700, "Jarrad" <> wrote:

>Ok so I took out all the inheritance parts and now have two template
>classes
>template <class T>
>class C1
>{
>void output();
>
>}
>
>template <class T>
>class C2
>{
>void output();
>
>}
>
>How ever I am still getting the error about C2 not being a tamplate
>type. I am starting to think that my error may be somewhere else.


You are typing semicolins after those class definitions, yes?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaring a template class with two template params a friend in anon-template class A L C++ 1 08-25-2010 07:25 AM
how to call an inherited, template class constructor from initializerlist of an inheriting, non-template class constructor l.s.rockfan@web.de C++ 4 11-15-2008 01:22 PM
how to call an inherited, template class constructor from initializerlist of an inheriting, non-template class constructor l.s.rockfan@web.de C++ 2 11-14-2008 10:04 PM
inheriting from a class template Alfonso Morra C++ 3 08-24-2005 10:15 AM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM



Advertisments