Template overloading

Discussion in 'C++' started by KK, Dec 1, 2005.

  1. KK

    KK Guest

    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
    KK, Dec 1, 2005
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Dec 1, 2005
    #2
    1. Advertising

  3. KK

    Ron Natalie Guest

    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) { ...
    Ron Natalie, Dec 1, 2005
    #3
  4. KK

    Guest

    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);
    > }
    , Dec 1, 2005
    #4
  5. KK

    Guest

    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);
    }
    , Dec 1, 2005
    #5
  6. KK

    Guest

    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);
    }
    , Dec 1, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Iyer, Prasad C

    Overloading __init__ & Function overloading

    Iyer, Prasad C, Sep 30, 2005, in forum: Python
    Replies:
    3
    Views:
    6,398
    Fredrik Lundh
    Sep 30, 2005
  2. Fredrik Lundh
    Replies:
    0
    Views:
    442
    Fredrik Lundh
    Sep 30, 2005
  3. Howard Gardner
    Replies:
    4
    Views:
    344
    Howard Gardner
    Jul 20, 2006
  4. Hicham Mouline
    Replies:
    0
    Views:
    426
    Hicham Mouline
    Apr 23, 2009
  5. Hicham Mouline
    Replies:
    1
    Views:
    409
    Michael DOUBEZ
    Apr 24, 2009
Loading...

Share This Page