Casting between different template classes

Discussion in 'C++' started by Mike Tyka, Sep 7, 2004.

  1. Mike Tyka

    Mike Tyka Guest

    Hello,

    I'd like to write a vector class using templates to be able to handle floats
    and doubles.
    I can't seem to find a way though to cast between the float type and the
    double type
    of the vector class. Is there a way of implementing the following
    mini-example neatly ??

    Thanks for any help,

    Mike

    //---------------------------------
    template <class T>
    class vector{
    public:
    T x,y;
    vector(T xinit,T yinit){ x=xinit;y=yinit; }
    void add(vector &v){
    x += v.x;
    y += v.y;
    }
    };

    void main(){
    vector <float> vector_float(2.0f,3.0f);
    vector <float> vector2_float(2.0f,1.0f);
    vector <double> vector_double(-1.0,3.0);

    // this works of course:
    vector_float.add(vector2_float);

    // this throws a compile error: - how can something like this be achieved
    ?
    vector_float.add(vector_double);
    }


    // the error thrown is:
    /*
    c:\coding\visc\mathtest\mathtest\main.cpp(26): error C2664: 'vector<T>::add'
    : cannot convert parameter 1 from 'vector<T>' to 'vector<T> &'
    with
    [
    T=float
    ]
    and
    [
    T=double
    ]
    and
    [
    T=float
    ]
    */
     
    Mike Tyka, Sep 7, 2004
    #1
    1. Advertising

  2. Mike Tyka wrote:
    > I'd like to write a vector class using templates to be able to handle floats
    > and doubles.
    > I can't seem to find a way though to cast between the float type and the
    > double type
    > of the vector class. Is there a way of implementing the following
    > mini-example neatly ??
    >
    > Thanks for any help,
    >
    > Mike
    >
    > //---------------------------------
    > template <class T>
    > class vector{
    > public:
    > T x,y;
    > vector(T xinit,T yinit){ x=xinit;y=yinit; }
    > void add(vector &v){
    > x += v.x;
    > y += v.y;
    > }


    Add this function:

    template<class U> void add(vector<U> const &vu) {
    x += vu.x;
    y += vu.y;
    }

    The "native" 'add' will be used for the same type of vector, and the
    templated 'add' will be used for "foreign" vectors.

    > };
    >
    > void main(){


    'main' _always_ returns an int.

    > vector <float> vector_float(2.0f,3.0f);
    > vector <float> vector2_float(2.0f,1.0f);
    > vector <double> vector_double(-1.0,3.0);
    >
    > // this works of course:
    > vector_float.add(vector2_float);
    >
    > // this throws a compile error: - how can something like this be achieved
    > ?
    > vector_float.add(vector_double);


    You can define your 'add' as a template. See above.

    > }
    >
    > [...]


    Victor
     
    Victor Bazarov, Sep 7, 2004
    #2
    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. Razvan
    Replies:
    5
    Views:
    11,316
    Dale King
    Jul 27, 2004
  2. kevin
    Replies:
    11
    Views:
    5,811
    Andrew McDonagh
    Jan 8, 2005
  3. Replies:
    8
    Views:
    1,363
    Victor Bazarov
    Jan 7, 2006
  4. Wally Barnes
    Replies:
    3
    Views:
    530
    Wally Barnes
    Nov 20, 2008
  5. Sosuke

    Up casting and down casting

    Sosuke, Dec 20, 2009, in forum: C++
    Replies:
    2
    Views:
    569
    James Kanze
    Dec 20, 2009
Loading...

Share This Page