overloaded operator =

Started by
10 comments, last by Empirical 20 years ago
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]
Advertisement

To convert automatically to an int:

operator int()
{ ... }
Brilliant! That worked perfect. Thanks!
You can't define operators that don't exist, or overload ( ?: ), ( . ), and ( :: ).

[edited by - ze_jackal on March 21, 2004 12:23:34 PM]
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!
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 Formats______________________________________________________________________________________
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 ...
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?

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.
Oh right, well, im sure it will be ok! Thanks all for your help.

This topic is closed to new replies.

Advertisement