In my program, I would like QString-s (from Qt) to be automatically
converted to std::string-s. The Qt people could have done this by
providing an operator std::string () inside class QString but they
didn't so I tried to do this using a global operator.
operator std::string (const QString & qs) { return qs.toStdString() ; }
but I got:
error: ‘operator std::string(const QString&)’ must be a nonstatic member
function
Whereas if I try:
const QString operator+ (const std::string & ss, const QString & qs) {
return QString::fromStdString(ss) + qs ;
}
it works. So what is special about operator othertype that it is not
allowed to be a non-member?
Similarly operator= is not allowed to be a non-member. So I cannot do:
const std::string & operator= ( std::string & ss, const QString & qs ) {
ss = qs.toStdString() ;
return ss ;
}
which is actually meaningful. Of course, since QString has toStdString,
I can always use that wherever I need to get a std::string from QString,
but operator overloading is a matter of convenience and I would like to
know if there is a strong reason that I cannot use operator othertype
and operator= as a non-member. It would enable me to define convenience
operators between objects of third-party (which includes the standard
library for me) types.
Shriramana Sharma.
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html