C++ cast operator overloads

Started by
18 comments, last by michael879 17 years, 5 months ago
Im making this matrix template class. I want it to be able to support any type as long as the proper operators are overloaded. One of these operators has to be the cast from int to the type T. This is because I have to cast (T)0 and (T)1 in order to make indentity and zero matrices. The problem is I have no idea how to define this cast for any non-native type (its default for native types). It has to be a static function, since any member cast overload is from that class to some variable type (I need it the other way around). any ideas?
Advertisement
:-) Im not sure how to define the cast but you dont actualy need to :-) you just use T(0) and T(1), then that calls the constructor of T which takes an int so for yout classes you just have a constructor like this...
class MyFloat{public:    MyFloat(int i);    // other stuff here};


Also the built in int defines a constructor which takes an int and float has a constructor that takes a float so the compiler will cast the 0 and 1 to floats for the float constructor (e.g. int(0) is 0 and float(0) is 0.0f), on that topic you could also do this...
class MyFloat{public:    MyFloat(float f);    // other stuff here};


and the compiler would cast 1 and 0 to floats and call the MyFloat(float f) constructor just like it will for the built in float.

Hope that helps
native types have default copy constructors? cool didnt know that. ok cool thanks I guess that works.

Im still kinda curious how to do this cast overload tho. I tried all the different syntaxes I could think of and nothing would compile.
you got me curious now so i went and tried this
class MyFloat{public:	MyFloat():	f(0)	{	}	MyFloat(int i):    f(i)	{	}	void Print()	{		std::cout << f;	}private:	float f;};template<typename T>class Matrix{public:	Matrix()	{		one = (T)1;	}	void Print()	{		one.Print();	}private:	T one;};int main(){	Matrix<MyFloat> m;	m.Print();	std::cin.get();}

and it seems to work so i think the syntax for a cast is actualy a constructor that tackes the thing being cast, any how if you try it let me know if it works plz.
wow rly? try casting without those constructors and see what happens. It might just be because the only private variable is a float that it knows how to cast and int into it.

also, I found a problem with using the constructor. Its pretty common for classes to have a constructor that takes a single int. what isnt common is overloading a cast operator (obviously since nobody seems to know how to do it). Thats why Id prefer to use the cast, so that there would be less conflicts in using this library.
Quote:Original post by michael879
Im making this matrix template class. I want it to be able to support any type as long as the proper operators are overloaded. One of these operators has to be the cast from int to the type T. This is because I have to cast (T)0 and (T)1 in order to make indentity and zero matrices.


That's a bad idea, and a poster child for misuse of the language. "(T)1" does not read as "give me an identity matrix" to anybody looking at the code. Instead, you should implement Matrix::Identity() as a static function that returns a reference to a static Matrix that's been initialized to the identity.

edit, Make that: you should implement Matrix::Identity() as a function that initializes and returns by value an identity matrix of the appropriate type and size.
I think you misread what Im trying to do. I have an identity function that turns a square matrix into an identity. What Im trying to do with (T)1 and (T)0 are allow the user to define what 1 and 0 mean.

for example: Im making a OpString class that is basically a string that keeps track of every operation done on it. (T)1 and (T)0 would become (OpString)1 and (OpString)0 so that I could make 0 -> "0" and 1 -> "1"

I am not trying to make a cast function to cast an int into a matrix, Im trying to make a cast function to make some type that is passed to the matrix from an int.

edit: my matrix is a template class that expects the type passed to it to have certain operators defined. I am trying to make one of these the cast operator.
I had a static indentity function also but I got rid of it for now.
I don't think it's possible to overload casts. I can think of two approaches here:
1) use the constructor as suggested by Julian90.
2) use helper template:
#include	<iostream>using std::cout;using std::endl;using std::ostream;template< typename T > T helper (int x) {	return static_cast<T>(x);}template< typename T > void TestHelper(int x = 0) {	T res = helper< T >(x);	cout << "x(" << x << ") converted to T: " << res << endl;}class A {public:	A(float _x = 0, float _y = 0) : x(_x), y(_y) {}	float x, y;};template<> A helper<A> (int x) {	return A(float(x), float(x));}ostream & operator << (ostream & stream, const A & a) {	return stream << "A(" << a.x << ", " << a.y << ")";}int main () {	cout << "T == int" << endl;	TestHelper< int >(1);	cout << "T == float" << endl;	TestHelper< float >(2);	cout << "T == class A" << endl;	TestHelper< A >(3);	return 0;}


With the second approach - defining a conversion is as simple as specializing template function.
I know its possible to overload casts. The syntax for the member function overload is:
class A
{
operator type();
}

where type is the name of some type. The problem is I dont want to cast from my defined type to a native type, I want to cast from a native type to my defined type.

Also, Im not rly sure I understand what you did in that code. Ive never used the special C++ cast functions (static, dynamic, forget the other 2).

This topic is closed to new replies.

Advertisement