C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Template Type Functions

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Mike
Guest





PostPosted: Thu Jan 27, 2005 9:57 pm    Post subject: Template Type Functions Reply with quote



I have a class temple which has to handle strings differently. Only a few
functions have to handle the string type differently. Is there anyway I can
do this without having two 'complete' classes, one generic and one for
strings only. I would just like to define the functions which are
different.

// Class Header example (there are other funcitons - this is just
// to show what I have done - and what I am trying to do.

tempate <class T>
class xList
{
public:
xList();
~xList();
T GetListVal();
void SetListVal( T );
private:
T *Ptr;
T Val;
};

tempate <>
class xList<std::string>
{
public:
xList();
~xList();
string GetListVal();
void SetListVal( string );
private:
string Val;
};

The only function that will be different is the 'string GetListVal()' - is
there anyway I can just define a special 'string' function for the
GetListVal() when the class is typed as string?

Thanks
Mike


Back to top
Victor Bazarov
Guest





PostPosted: Thu Jan 27, 2005 10:20 pm    Post subject: Re: Template Type Functions Reply with quote



Mike wrote:
Quote:
I have a class temple which has to handle strings differently. Only a few
functions have to handle the string type differently. Is there anyway I can
do this without having two 'complete' classes, one generic and one for
strings only. I would just like to define the functions which are
different.

// Class Header example (there are other funcitons - this is just
// to show what I have done - and what I am trying to do.

tempate <class T
class xList
{
public:
xList();
~xList();
T GetListVal();
void SetListVal( T );
private:
T *Ptr;
T Val;
};

tempate
class xList {
public:
xList();
~xList();
string GetListVal();
void SetListVal( string );
private:
string Val;
};

The only function that will be different is the 'string GetListVal()' - is
there anyway I can just define a special 'string' function for the
GetListVal() when the class is typed as string?

You may provide an explicit specialisation of that function for 'string'
type instead of the whole class:

template<> string xList<string>::GetListVal() {
blah blah blah;
return somestring;
}

The rest of the class implementation will be picked by instantiating the
template members.

V

Back to top
Mike
Guest





PostPosted: Fri Jan 28, 2005 3:14 am    Post subject: Re: Template Type Functions Reply with quote




Quote:
Mike wrote:
I have a class temple which has to handle strings differently. Only a
few functions have to handle the string type differently. Is there
anyway I can do this without having two 'complete' classes, one generic
and one for strings only. I would just like to define the functions
which are different.

// Class Header example (there are other funcitons - this is just
// to show what I have done - and what I am trying to do.

tempate <class T
class xList
{
public:
xList();
~xList();
T GetListVal();
void SetListVal( T );
private:
T *Ptr;
T Val;
};

tempate
class xList {
public:
xList();
~xList();
string GetListVal();
void SetListVal( string );
private:
string Val;
};

The only function that will be different is the 'string GetListVal()' -
is there anyway I can just define a special 'string' function for the
GetListVal() when the class is typed as string?

"Victor Bazarov" wrote:

Quote:
You may provide an explicit specialisation of that function for 'string'
type instead of the whole class:

template<> string xList<string>::GetListVal() {
blah blah blah;
return somestring;
}

The rest of the class implementation will be picked by instantiating the
template members.

V

Thanks for response, that is exactly what I was looking for.

Mike



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.