Operator= Overloading
-
Hi All, I'm trying to overload the assignment (=) operator so that I can achieve the following: int x = myclass; instead of: int x = myclass.Value(); I am implementing it as follows: class CMyClass { private: int m_nValue; public: int& operator=(CMyClass& class); int Value() { return m_nValue; }; }; int& CMyClass::operator=(CMyClass& class) { return class.Value(); } But I keep on getting a compiler errors either saying that there is no suitable conversion, or that it cannot convert from CMyClass to int. Clearly my syntax (or something) is screwed, so how can I fix it ? Thanks OD
-
Hi All, I'm trying to overload the assignment (=) operator so that I can achieve the following: int x = myclass; instead of: int x = myclass.Value(); I am implementing it as follows: class CMyClass { private: int m_nValue; public: int& operator=(CMyClass& class); int Value() { return m_nValue; }; }; int& CMyClass::operator=(CMyClass& class) { return class.Value(); } But I keep on getting a compiler errors either saying that there is no suitable conversion, or that it cannot convert from CMyClass to int. Clearly my syntax (or something) is screwed, so how can I fix it ? Thanks OD
Write a casting operator instead:
operator int() const { return m_nValue; }
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown -
Hi All, I'm trying to overload the assignment (=) operator so that I can achieve the following: int x = myclass; instead of: int x = myclass.Value(); I am implementing it as follows: class CMyClass { private: int m_nValue; public: int& operator=(CMyClass& class); int Value() { return m_nValue; }; }; int& CMyClass::operator=(CMyClass& class) { return class.Value(); } But I keep on getting a compiler errors either saying that there is no suitable conversion, or that it cannot convert from CMyClass to int. Clearly my syntax (or something) is screwed, so how can I fix it ? Thanks OD
od@ananzi.co.za wrote:
I'm trying to overload the assignment (=) operator so that I can achieve the following: int x = myclass; instead of: int x = myclass.Value();
If I got your intention correctly, you need
operator int
, notoperator =
-
Write a casting operator instead:
operator int() const { return m_nValue; }
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown -
Nemanja Trifunovic wrote:
Damn, you were faster :)
Hehe, sometimes I get lucky. ;) Haven't refreshed in about two hours. It was a good thing that we suggested the same thing though...
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown -
Write a casting operator instead:
operator int() const { return m_nValue; }
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknownThanks, worked like a charm !