UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C++
  • » about template in Inheritance

#1 2005-11-30 12:37 PM

enjoy
Member
Registered: 2005-03-13
Posts: 20

Re: about template in Inheritance

Hi,

     have a look at my code,

class A{
              private:
                               template <class T> a;
                               template <class T> b;
                               template <class T> c;
                               int  count
                             
              public :
                             template <class T> void add(T a,T b){}
                              void sub(---){}
                              template <class T> mul(T a, T b){}
};


class B: public A{
                        B():A(){---}
----
};


so here i wanted to make few data members from class A as template type and also the functions.but not all.I have inherited that class (A) into another class.

The above declarations are not working. The problem comes in the declaration of data members from class A.

Is it not like as the function declarations while handling templates?

regards
Enjoy.

Offline

#2 2006-02-25 05:19 PM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: about template in Inheritance

Better giving an answer late than never I always say... and besides, this topic seemed lonely...


I think... not certain but think... what you wanted to write was soemthing along the lines of:

template <typename T> class A
{
	private:
		T a;
		T b;
		T c;

	public :
		T add(T a,T b){}
		T mul(T a, T b){}
};


You'll notice that I have:

* changed class A to a template class...

* changed the class A functions to use the type parameter and removed extraneous segments...

* changed the template parameter type from class to typename which is a more common convention...


The reason I changed the functions is that a template function ( using one of yours ) declared in the form of:

template <typename T> void add(T a,T b){}

is really used most often when creating a function template that is stand alone... that is, not contained in a class... and if you did want it embedded in a template class you would need to change the type ( T ) so that it didn't conflict with the type ( T ) for the enclosing template class...


Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

  • Index
  • » C++
  • » about template in Inheritance

Board footer

Powered by FluxBB