C++ operator
-
Hi, everybody, i need some help for writing an operator= ::::::::::::::::::::::::::::::: class aa { public: int var; virtual aa& operator=(const aa && c ) throw(); }; ::::::::::::::::::::::::::::::: class bb : public aa { public: int varbb; bb& operator=(const bb && c ) { varbb=c.varbb; return *this; }; }; int main() { aa * xx = new bb(); aa * yy = new bb(); *xx=*yy; }; Problem: Ok, so when i execute this code "*xx=*yy" the aa operator= was called, but i want to use the bb operator=. How can i do that? Ho, sorry for my english.
-
Hi, everybody, i need some help for writing an operator= ::::::::::::::::::::::::::::::: class aa { public: int var; virtual aa& operator=(const aa && c ) throw(); }; ::::::::::::::::::::::::::::::: class bb : public aa { public: int varbb; bb& operator=(const bb && c ) { varbb=c.varbb; return *this; }; }; int main() { aa * xx = new bb(); aa * yy = new bb(); *xx=*yy; }; Problem: Ok, so when i execute this code "*xx=*yy" the aa operator= was called, but i want to use the bb operator=. How can i do that? Ho, sorry for my english.
In that case you must declare the operator in class bb the same way than in class aa. Did you notice that they are not compatible? They both must be virtual. (In fact, that's the first time I saw someone using virtual operator but hey, it's good to know. Thanks).
-
Hi, everybody, i need some help for writing an operator= ::::::::::::::::::::::::::::::: class aa { public: int var; virtual aa& operator=(const aa && c ) throw(); }; ::::::::::::::::::::::::::::::: class bb : public aa { public: int varbb; bb& operator=(const bb && c ) { varbb=c.varbb; return *this; }; }; int main() { aa * xx = new bb(); aa * yy = new bb(); *xx=*yy; }; Problem: Ok, so when i execute this code "*xx=*yy" the aa operator= was called, but i want to use the bb operator=. How can i do that? Ho, sorry for my english.
Since the bb operator is not declared exactly as the aa operator, they're not treated polymorphically. In other words, they appear as two different functions and the compiler uses the one that most closely matches. Regards, Alvaro
-
Since the bb operator is not declared exactly as the aa operator, they're not treated polymorphically. In other words, they appear as two different functions and the compiler uses the one that most closely matches. Regards, Alvaro
-
Hi, everybody, i need some help for writing an operator= ::::::::::::::::::::::::::::::: class aa { public: int var; virtual aa& operator=(const aa && c ) throw(); }; ::::::::::::::::::::::::::::::: class bb : public aa { public: int varbb; bb& operator=(const bb && c ) { varbb=c.varbb; return *this; }; }; int main() { aa * xx = new bb(); aa * yy = new bb(); *xx=*yy; }; Problem: Ok, so when i execute this code "*xx=*yy" the aa operator= was called, but i want to use the bb operator=. How can i do that? Ho, sorry for my english.
* reinterpret_cast<bb *>(xx) = * reinterpret_cast<bb *>(yy);
Bomb our homes and threaten our children, and, as difficult as it is, we will still love you --- Martin Luther King, Jr. -
* reinterpret_cast<bb *>(xx) = * reinterpret_cast<bb *>(yy);
Bomb our homes and threaten our children, and, as difficult as it is, we will still love you --- Martin Luther King, Jr. -
The solution is to add another assignment operator inside bb that looks exactly like the one in aa. Then you can call bb operator from there by casting the parameter to bb&:
virtual aa& operator=(const aa& c) throw()
{
return operator=((bb&)c);
}Regards, Alvaro
-
Hi, everybody, i need some help for writing an operator= ::::::::::::::::::::::::::::::: class aa { public: int var; virtual aa& operator=(const aa && c ) throw(); }; ::::::::::::::::::::::::::::::: class bb : public aa { public: int varbb; bb& operator=(const bb && c ) { varbb=c.varbb; return *this; }; }; int main() { aa * xx = new bb(); aa * yy = new bb(); *xx=*yy; }; Problem: Ok, so when i execute this code "*xx=*yy" the aa operator= was called, but i want to use the bb operator=. How can i do that? Ho, sorry for my english.
Can operators even be virtual? You certainly can't make = virtual, because = always operates on a object, not a pointer to an object, as is required for polymorphism. --Mike-- http://home.inreach.com/mdunn/ Help! Help! I'm being repressed!! :love: your :bob: with :vegemite: and :beer: Sonork - 100.10414 AcidHelm