overloaded operator =

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

This topic is closed to new replies.

Advertisement