thnx a ton for the earlier reply to my post regarding constructors.......it helped me a lot
i have another query......in the following snippet the overloaded assignment operator=(with const argument as reference to object) is invoked everytime i assign either a const* or an int to the object without checking its type.
however the code doesnt compile when i remove the const prefix in the overloaded assignment operator argument and it gives me a type mismatch error.
Nevertheless if i change the argument from reference to object to just an object i.e.
from
operator=(abc& ref)
to
operator=(abc ref)
......the code compiles well.........why doesnt it give the type mismatch error now????
please help me on this issue?........any links to furthur reading material as well on similar issue would be greatly appreciated
Code:#include<iostream> using namespace std; class abc{ public: abc(){}; abc(char* word){ MemberVar = word; } abc(int num){ } abc& operator=(const abc& ref) { return(*this); } private: char *MemberVar; }; int main(){ abc object("Hello"); abc anotherObject; anotherObject = "i Know why this works now!!!";// anotherObject = 8;//But i dunno why this compiles return 0; }




Reply With Quote