• Announcements

Archived

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

Recommended Posts

Empirical    190
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 on other sites
Guest Anonymous Poster

To convert automatically to an int:

operator int()
{ ... }

Share on other sites
Empirical    190
Brilliant! That worked perfect. Thanks!

Share on other sites
jdhardy    469
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 on other sites
JohnBolton    1372
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 on other sites
Thunder_Hawk    314
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 on other sites
Xai    1838
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 on other sites
Empirical    190
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 on other sites
SiCrane    11839
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 on other sites
Empirical    190
Oh right, well, im sure it will be ok! Thanks all for your help.

Share on other sites
fallenang3l    144
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 on other sites
petewood    819
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 Ttemplate<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]