Overloading -> and ->* operators
-
I'm sure there's a C++ expert who can explain who to overload the indirection to a member and deferencing a pointer.
class A{ int m_i; float *m_pf };
My questions are how to use operator -> to return m_i, ->* to return m_pf. What parameters to I use when create the method. Does it work when you create a pointer to an object of type classA? -
I'm sure there's a C++ expert who can explain who to overload the indirection to a member and deferencing a pointer.
class A{ int m_i; float *m_pf };
My questions are how to use operator -> to return m_i, ->* to return m_pf. What parameters to I use when create the method. Does it work when you create a pointer to an object of type classA?Well techincally it would be like this: class A { public: int m_i; float *m_pf; int operator->() const { return m_i; } float& operator*() const { return *m_pf; } }; But - while the * operator makes sense, the -> makes none at all. Why would you want a -> operator to return a basic type? (ie not a pointer to a class) In fact, I belive if you compile the above you will get: warning C4284: return type for 'A::operator ->' is 'int' (ie; not a UDT or reference to a UDT. Will produce errors if applied using infix notation) return type for 'identifier::operator –>' is not a UDT or reference to a UDT. Will produce errors if applied using infix notation The operator–>( ) function must return either a pointer to a class or an object of or a reference to a class for which operator–>( ) is defined. The following example causes this warning: class C { public: int operator->(); // warning }; It is meaningless to apply the member access operator to fundamental data types that have no member access. This warning message will appear when this happens.