Classes that reference classes

Pages: 12
That's a good idea. I tend to ignore the menu options for creating classes and things like that because I prefer to actaully write them, but if it gets rid of problems like this then that can only be good. What's the syntax for defining/declaring an operator (like +) for a class?
Last edited on
Last edited on
That's very helpful. The only problem I can think of at the moment is that I wanted to make classes so that it could be limited int or limited unsigned or limited unsigned char. I could make all these individually apart from that fact that I don't know how to make the name of a class include spaces. Is it possible, or would it have to be limited_int? Also, I've just made four operator overloads:
1
2
3
4
int operator+ (const limited_int& other);
int operator+ (const int& other);
limited_int operator+ (const limited_int& other);
limited_int operator+ (const int& other);


But it comes up with an error saying that the third one cannot be overloaded with the first one and the fourth one cannot be overloaded with the second. How can I set it up so that all of th following are possible:
int = limited_int + limited_int;
limited_int = limited_int+limited_int;
int = limited_int + int;
limited_int = limited_int + int;
I'm thinking that I need to make it able to convert from limited_int to int (very easy, just return the value variable) but I don't know how.

EDIT: I think I've done it using operator int() {return value;}.
In case anyone else has the same problem, this is a really good link http://en.cppreference.com/w/cpp/language/cast_operator
Last edited on
Topic archived. No new replies allowed.
Pages: 12