Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Casting between different template classes

Reply
Thread Tools

Casting between different template classes

 
 
Mike Tyka
Guest
Posts: n/a
 
      09-07-2004
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
]
*/


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-07-2004
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
 
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
Up casting and down casting Sosuke C++ 2 12-20-2009 03:24 PM
Problem with depracated casting method (down casting) Wally Barnes C++ 3 11-20-2008 05:33 AM
types of classes in template classes lobequadrat@googlemail.com C++ 5 04-27-2007 05:33 PM
OOP casting classes (using one single variable for similar types of classes) roberts.noah@gmail.com C++ 8 01-07-2006 04:13 PM
Another question about inheritance (up-casting and down-casting) kevin Java 11 01-08-2005 07:11 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57