By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,134 Members | 1,796 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,134 IT Pros & Developers. It's quick & easy.

Template Type Functions

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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<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?


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
Jul 23 '05 #2

P: n/a
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<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?

"Victor Bazarov" wrote:
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
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.