Template overloading

K

KK

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
 
V

Victor Bazarov

KK said:
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)

void Example said:
{
_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)

void Example said:
{
_val = T (a+1);
}

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

What you do is _specialise_:

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

V
 
R

Ron Natalie

KK wrote:
KK said:
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);
}
template <> void Init<Float>(int a) { ...
 
R

roberts.noah

KK said:
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
edit:

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

hgupta

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);
}
 
H

hgupta

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);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top