Archived

This topic is now archived and is closed to further replies.

Empirical

overloaded operator =

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

To convert automatically to an int:

operator int()
{ ... }

Share this post


Link to post
Share on other sites
You can't define operators that don't exist, or overload ( ?: ), ( . ), and ( :: ).

[edited by - ze_jackal on March 21, 2004 12:23:34 PM]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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
______________________________________________________________

Share this post


Link to post
Share on other sites
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 ...

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites