Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 1st, 2005, 08:55 PM
KK
Guest
 
Posts: n/a
Default 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

  #2  
Old December 1st, 2005, 09:15 PM
Ron Natalie
Guest
 
Posts: n/a
Default 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
Default 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
Default 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
Default 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
Default 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
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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.
Post your question now . . .
It's fast and it's free

Popular Articles