Connecting Tech Pros Worldwide Help | Site Map

Template overloading

  #1  
Old December 1st, 2005, 08:55 PM
KK
Guest
 
Posts: n/a
Hello all,
I'm sure this must be a simple matter of language syntax, but could
not find an appropriate reference so I'm posting the question here.
Consider

template < class T = double >
class Example
{
T _val;
//define constructor & destructor
void Init ( int a );
}

template <class T > //do this if T is of type double, int, unsigned
void Init (int a)
{
_val = T(a);
}

so far it is fine ... I would like to overload function 'Init' when the
'T' is type 'float' with something like

template <class T >
void InitFloat (int a)
{
_val = T (a+1);
}

how can I achieve both functions exist with same function name?

Thank you
-KK

  #2  
Old December 1st, 2005, 09:15 PM
Ron Natalie
Guest
 
Posts: n/a

re: Template overloading


KK wrote:
KK wrote:[color=blue]
> Hello all,
> I'm sure this must be a simple matter of language syntax, but could
> not find an appropriate reference so I'm posting the question here.
> Consider
>
> template < class T = double >
> class Example
> {
> T _val;
> //define constructor & destructor
> void Init ( int a );
> }
>
> template <class T > //do this if T is of type double, int, unsigned
> void Init (int a)
> {
> _val = T(a);
> }
>
> so far it is fine ... I would like to overload function 'Init' when the
> 'T' is type 'float' with something like
>
> template <class T >
> void InitFloat (int a)
> {
> _val = T (a+1);
> }
>[/color]
template <> void Init<Float>(int a) { ...
  #3  
Old December 1st, 2005, 09:15 PM
roberts.noah@gmail.com
Guest
 
Posts: n/a

re: Template overloading



KK wrote:[color=blue]
> Hello all,
> I'm sure this must be a simple matter of language syntax, but could
> not find an appropriate reference so I'm posting the question here.
> Consider
>
> template < class T = double >
> class Example
> {
> T _val;
> //define constructor & destructor
> void Init ( int a );
> }
>
> template <class T > //do this if T is of type double, int, unsigned
> void Init (int a)
> {
> _val = T(a);
> }
>
> so far it is fine ... I would like to overload function 'Init' when the
> 'T' is type 'float' with something like
>[/color]
edit:
[color=blue]
> template <>
> void Init<float> (int a)
> {
> _val = T (a+1);
> }[/color]

  #4  
Old December 1st, 2005, 09:15 PM
hgupta@bearwagner.com
Guest
 
Posts: n/a

re: Template overloading


try this.

Hitendra

template < class T = double >
class Example
{
T _val;
//define constructor & destructor
public:
void Init ( int a );
};

template <class T > //do this if T is of type double, int, unsigned
void Example<T>::Init (int a)
{
cout << "Init for non float" << endl;
_val = T(a);
}

void Example<float>::Init (int a)
{
cout << "Init for float" << endl;
_val = T (a+1);
}

  #5  
Old December 1st, 2005, 09:15 PM
hgupta@bearwagner.com
Guest
 
Posts: n/a

re: Template overloading


try this.

Hitendra

template < class T = double >
class Example
{
T _val;
//define constructor & destructor
public:
void Init ( int a );
};

template <class T > //do this if T is of type double, int, unsigned
void Example<T>::Init (int a)
{
cout << "Init for non float" << endl;
_val = T(a);
}

void Example<float>::Init (int a)
{
cout << "Init for float" << endl;
_val = T (a+1);
}

  #6  
Old December 1st, 2005, 09:15 PM
Victor Bazarov
Guest
 
Posts: n/a

re: Template overloading


KK wrote:[color=blue]
> Hello all,
> I'm sure this must be a simple matter of language syntax, but could
> not find an appropriate reference so I'm posting the question here.
> Consider
>
> template < class T = double >
> class Example
> {
> T _val;
> //define constructor & destructor
> void Init ( int a );
> }
>
> template <class T > //do this if T is of type double, int, unsigned
> void Init (int a)[/color]

void Example<T>::Init(int a)
[color=blue]
> {
> _val = T(a);
> }
>
> so far it is fine ... I would like to overload function 'Init' when the
> 'T' is type 'float' with something like
>
> template <class T >
> void InitFloat (int a)[/color]

void Example<T>::InitFloat(int a)
[color=blue]
> {
> _val = T (a+1);
> }
>
> how can I achieve both functions exist with same function name?[/color]

What you do is _specialise_:

template<>
void Example<float>::Init(int a)
{
_val = a+1;
}

V
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
template overloading operator c++ 6 to vs2005 AlanJSmith answers 4 October 6th, 2006 08:15 AM
Function template + overloading + polymorphism Imre answers 4 July 23rd, 2005 02:49 AM
function template overloading and typedef in GCC Arkadiy Vertleyb answers 5 July 19th, 2005 07:29 PM
template overloading / nested class Alexander Stippler answers 3 July 19th, 2005 04:41 PM