
December 1st, 2005, 08:55 PM
| | | Template overloading
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 | 
December 1st, 2005, 09:15 PM
| | | 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) { ... | 
December 1st, 2005, 09:15 PM
| | | 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] | 
December 1st, 2005, 09:15 PM
| | | 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);
} | 
December 1st, 2005, 09:15 PM
| | | 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);
} | 
December 1st, 2005, 09:15 PM
| | | 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 |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|