Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Template overloading

Reply
Thread Tools

Template overloading

 
 
KK
Guest
Posts: n/a
 
      12-01-2005
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-01-2005
KK wrote:
> 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<T>::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)


void Example<T>::InitFloat(int a)

> {
> _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
 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      12-01-2005
KK wrote:
KK wrote:
> 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) { ...
 
Reply With Quote
 
roberts.noah@gmail.com
Guest
Posts: n/a
 
      12-01-2005

KK wrote:
> 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);
> }


 
Reply With Quote
 
hgupta@bearwagner.com
Guest
Posts: n/a
 
      12-01-2005
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);
}

 
Reply With Quote
 
hgupta@bearwagner.com
Guest
Posts: n/a
 
      12-01-2005
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);
}

 
Reply With Quote
 
 
 
Reply

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
overloading non-template member functions with template member functions Hicham Mouline C++ 1 04-24-2009 07:47 AM
overloading non-template member functions with template member functions Hicham Mouline C++ 0 04-23-2009 11:42 AM
overloading on the template parameter arity of a template templateparameter Howard Gardner C++ 4 07-19-2006 11:10 PM
Overloading __init__ & Function overloading Iyer, Prasad C Python 3 09-30-2005 02:17 PM
Re: Overloading __init__ & Function overloading Fredrik Lundh Python 0 09-30-2005 01:53 PM



Advertisments