Skip to main content
GameDev.net gamedev.net
🔒 Locked

overloaded operator =

Started by Empirical Mar 21, 2004 at 10:49 AM 10 replies 650+ views
Original Post
Empirical
Empirical
Im creating my own string class, i have searched and it seems what i want to do is not possible, but just in case i thought id ask anyway I can do this STRING String1=Value; (where value is an int) I want to be able to use the following expression on my string class int Value = String1; where String1 is of class string. But because i also have an operator for (two STRING classes) it cannot pick between them (only return type differs) STRING String1 = String2; Is there any way to overload the = operator to take into account the return type? And whilst im posting, is there anyway to define a brand new operator symbol? (e.g STRING String1 $ String2) where $ is not currently a valid operator. Does any of this even make sence? [edited by - empirical on March 21, 2004 11:51:11 AM]
Empirical
Empirical
Brilliant! That worked perfect. Thanks!
jdhardy
jdhardy
You can't define operators that don't exist, or overload ( ?: ), ( . ), and ( :: ).

[edited by - ze_jackal on March 21, 2004 12:23:34 PM]
JohnBolton
JohnBolton
quote:
Original post by Anonymous Poster

To convert automatically to an int:

operator int()
{ ... }



Be careful when doing this because the compiler will use it to convert Strings to ints when you least expect it and even when you don''t want it to.

The alternative is to create a member function (named something like to_int) that returns the int value. It doesn''t look as as clever, but it is safer.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Thunder_Hawk
Thunder_Hawk
A direct conversion like that tends to die very quickly if you have pointers to your objects.

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Forma
Xai
Xai
NOTICE ... C++ has TWO mechanisms to convert types ...

Constructors with single paramters are used for type conversion.

overloaded cast operators are used for type conversion.

both do basically the same thing, just with different syntax - and different rules about WHO (code wise) is doing the adding.

but if you use BOTH ambiguity can arise ... (also, I think "Effective C++" mentions this)

if you put:

operator int()

inside your STRING class, your code now works .. and so does the cast operator:

(int) someString;
// and
static_cast(someString);

if int we''re not a built in type, but something like Integer lets say, you could add a constructor:
Integer::Integer(STRING s);

then these are all valid:

Integer(someString)
(Integer)someString
static_cast(someString)

BUT, if you have both ... the compiler cannot figure out which to use ...
Empirical
Empirical
quote:
Original post by JohnBolton
quote:
Original post by Anonymous Poster

To convert automatically to an int:

operator int()
{ ... }



Be careful when doing this because the compiler will use it to convert Strings to ints when you least expect it and even when you don''t want it to.



In what way?

SiCrane
SiCrane
Well, let''s pretend that you''re going to send your string over a socket. So you might do something like:

send(my_socket, my_string.data(), my_string.length(), 0);

But, let''s pretend you got distracted and accidently typed:

send(my_socket, my_string.data(), my_string, 0);

This is probably won''t do what you want to happen, but the compiler won''t give off any warnings.
Empirical
Empirical
Oh right, well, im sure it will be ok! Thanks all for your help.
fallenang3l
fallenang3l
quote:
Original post by Empirical
Oh right, well, im sure it will be ok! Thanks all for your help.


Trust us, it won''t be ok. Use an explicit conversion function and bulletproof your design. There are only a handful of situations where implicit conversion operators are useful, and this is not one of them.
Qw3r7yU10p!
Qw3r7yU10p!
quote:
Original post by Empirical
Oh right, well, im sure it will be ok! Thanks all for your help.


Trust us. It won't be alright.

Instead have a non-member function so that you can convert from a string to whatever you want:

//convert string to type T

template<class T>
T to(std::string& str) {
T result;
std::stringstream ss;
ss << str;
ss >> result;
return result;
}

int i = to<int>("10");
float f = to<float>("11.5f");
double d = to<double>("3.142");
complex<double> c = to<complex<double> >("(3.2, 7.5)");

These functions have already been written for you in boost.lexical_cast. read up on it

[edited by - petewood on March 22, 2004 6:23:03 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.