So, after researching everywhere for it, I cannot seem to find how to create a class arrow operator, i.e.,
class Someclass
{
operator-> () /* ? */
{
}
};
I just need to know how to work with it and use it appropriately. - what are its inputs? - what does it return? - how do I properly declare/prototype it?
The "arrow" operator can be overloaded by:
will be translated to
The arrow operator has no inputs. Technically, it can return whatever you want, but it should return something that either is a pointer or can become a pointer through chained
->
operators.The
->
operator automatically dereferences its return value before calling its argument using the built-in pointer dereference, notoperator*
, so you could have the following class:Use it like:
which is converted by the compiler into:
(with as many
.operator->()
as necessary to return a real pointer) and should outputNote, however, that you can do the following:
run online: https://wandbox.org/permlink/Is5kPamEMUCA9nvE
From Stroupstrup:
http://en.wikibooks.org/wiki/C++_Programming/Operators/Operator_Overloading#Address_of.2C_Reference.2C_and_Pointer_operators
The operator -> is used to overload member access. A small example:
This outputs: