Why ? What do you expect to get with for example "x" + "y" other than "xy" ?
If you need custom behaviour just use custom methods, like
QString myPlus
(const QString
& a,
const QString
& b
)
QString myPlus(const QString& a, const QString& b)
To copy to clipboard, switch view to plain text mode
.
You cannot provide new declarations for functions / classes etc. that are already defined.
If you really want, you can implement the operator+ as a member method of your class, like:
class Test{
public:
Test(const QString& s){
buff = s;
}
QString operator
+(const QString
& s
){ return buff+s;
}
private:
};
int main(int argc, char ** argv)
{
Test t("x");
qDebug() << t+"y";
return 0;
}
class Test{
public:
Test(const QString& s){
buff = s;
}
QString operator+(const QString& s){
return buff+s;
}
private:
QString buff;
};
int main(int argc, char ** argv)
{
Test t("x");
qDebug() << t+"y";
return 0;
}
To copy to clipboard, switch view to plain text mode
or as non-member function and make it a "friend" of the class...
I don't know, personally I just avoid overloading operators.
Btw. It is never a good idea to modify something that is well defined. This is probably your personal project, but imagine that someone will someday work on this code, he'd be very surprised to find that "newString = string1+string2" is not really what its supposed to be, or have a nasty side effects...
Bookmarks