Reputation: 947
I saw the following example in book of the C++ Programming Language
class Ptr {
X* operator->( );
};
voide f(Ptr p)
{
p->m=7;
X* q1 = p->;
X* q2 = p.operator->();
}
The book claims that 1)Objects of class Ptr can be used to access members of class X in a very similar manner to the way pointers are used. 2)The transformation of the object p into the pointer p.operator->() does not depend on the member m pointed to. That is the sense in which operator->( ) is a unary postfix operator.
For the first point, I do not understand why we need to this design, or in which scenarios should use this kind of design. For the second point, I am confused about the message that the author want to deliver.
Thanks.
Upvotes: 2
Views: 614
Reputation: 9943
iterators... a fundamental part of the C++ standard library
.
std::list<std::string> list_of_string
std::list<std::string>::iterator i;
for(i = list_of_string.begin(); i != list_of_string.end(); ++i)
{
//WHERE -> IS OVERLOADED...
printf("%s\n", i->c_str()); //print to the screen, printf() is C and requires a C-string.
}
normally, i
should be thought of as a std::list<>
object, not a std::string
. Normally c_str()
would be inaccessible too, without the -> overloaded.
Upvotes: 2
Reputation: 126867
This design is extremely useful when we want to create a class that behaves like a pointer; this is the case of smart pointers (objects that have a pointer-like interface but that provide additional "services", e.g. automatic deallocation on destruction, ownership transfer, reference counting, ...).
Notice that often this kind of object will also overload the *
(dereference) operator to mimic pointers more closely.
The author wants to say that when you use the ->
operator on Ptr
, it's not relevant (as far as operator->
is concerned) what you put after it: in any case, the operator->
method will be called, that will return a pointer to the object that will be considered for the rest of the expression.
To make this more clear: quoting directly from the standard:
13.5.6 Class member access
operator->
shall be a non-static member function taking no parameters. It implements class member access using->
postfix-expression -> id-expression
An expression
x->m
is interpreted as(x.operator->())->m
for a class objectx
of typeT
ifT::operator->()
exists and if the operator is selected as the best match function by the overload resolution mechanism (13.3).
In other words:
operator->()
directly (second and third example in the OP code), you will get the pointer returned by the operator->
method, just like what happens with any method;->
operator (e.g. x->m
, as in the first example in the OP code), the overloaded operator->
will be called, and the returned pointer will be used as if the ->m
was being used over it.Upvotes: 3
Reputation: 25543
This operator is overloaded when implementing objects that behave like (pretend to be) pointers.
A good example would be boost::shared_ptr
, which is a classic reference counting pointer that automatically deletes the pointed-to object when all pointers are destroyed. Countless other similar "smart pointer" objects have been implemented by people for all sorts of reasons.
(and, as pointed out the stl iterators also use this to behave like pointers, allowing syntax like it->method();
or it->data;
)
Upvotes: 5
Reputation: 44308
it allows for creating smart pointers. (little objects that behave just like normal pointers)
The main reason for this is automatic memory management. But can also be used for things like proxys
Upvotes: 1