How to overload -> operator in C++ [duplicate] - c++

This question already has answers here:
Overload -> operator to forward member-access through Proxy
(3 answers)
Overloading member access operators ->, .*
(5 answers)
Closed 8 years ago.
As per the title, how to overload -> operator in C++?
I can't find any documentation.
cppreference glosses over it.
The Wikipedia page on overloading similarly glosses over it.
Operator overloading <-- this SO post again has a '->' shaped hole in it, although one comment gives a hint:
operator->() is actually extremely weird. It's not required to return
a value_type* -- in fact, it can return another class type, provided
that class type has an operator->(), which will then be called
subsequently. This recursive calling of operator->()s proceeds until a
value_type* return type occurs. Madness! :)
It appears that this particular operator is not straightforward to overload.
Could someone link to documentation, or (preferably) provide some here?
PS I recognisze this is an unusual overload, I have need of it in a Proxy pattern, here

Related

What Parameter Types are allowed for overloaded != function in a class [duplicate]

This question already has answers here:
What are the basic rules and idioms for operator overloading?
(8 answers)
Closed 4 years ago.
Just for learning purposes and if I would really like to compare apples with pies. And ignoring for the moment rules and idioms
What type of parameter is allowed for overloaded operators, e.g. "operator !=" ?
Ok, it is expecting exactly one parameter, but what type?. Having read 'functions', 'declarations', 'types' and 'operator overloading', I still do not know it. Any restriction? Maybe the same as any other function? Can somebody confirm that?
Any help appreciated
Edit:
In my post I even linked the proposed dup. So, why mark it as dup. I read that thread before. My question is nowhere answered in the post. I am not sure how to rephrase the question. It is about the "C++ type" of the parameter in the overloaded operator function. A "C++ type" is something like described in types. For example, I think that the type must be complete. Any other restrictions or requirements?
What type of parameter is allowed for overloaded operators, e.g. "operator !=" ?
There are very few restrictions, the most important being: you cannot overload operators for builtin types. So one of the two types for binary operators must be a user-defined type.
[over.oper]/6
An operator function shall either be a non-static member function or be a non-member function that has at least one parameter whose type is a class, a reference to a class, an enumeration, or a reference to an enumeration. It is not possible to change the precedence, grouping, or number of operands of operators.
According to cppreference, the function signature (for a free function) is:
bool operator !=(const T &a, const T2 &b);
Where:
T2 can be any type including T
Similar wording applies if operator!= is a class member function. Plus what YSC said.

In c++ why we can overload `operator ->` and can't overload `operator .` [duplicate]

This question already has answers here:
Why can't you overload the '.' operator in C++?
(4 answers)
Closed 5 years ago.
In c++, why we can overload operator -> and can't overload operator .?
Similarly, why we can overload operator ->* and can't overload operator .*?
I will be appreciated if you can help me!
Stroustrup actually has this as an FAQ question on his website. To quote his answer:
Operator . (dot) could in principle be overloaded using the same technique as used for ->. However, doing so can lead to questions about whether an operation is meant for the object overloading . or an object referred to by .
This problem can be solved in several ways. At the time of standardization, it was not obvious which way would be best
(Note that Stroustup himself has since proposed allowing overloading of operator., as have others, so we may see it in a future version of the language.)

Why can't a ternary operator be overloaded? [duplicate]

This question already has answers here:
Why is it not possible to overload the ternary operator?
(6 answers)
Closed 5 years ago.
I was always curious about that, but never found a solid answer or an easy to understand explanation. I have tried with other operators and it works just fine. This operator in particular is an interesting one. I just can't get my head wrap around it.
FAQ of stroustrup :
There is no fundamental reason to disallow overloading of ?:. I just
didn't see the need to introduce the special case of overloading a
ternary operator. Note that a function overloading expr1?expr2:expr3
would not be able to guarantee that only one of expr2 and expr3 was
executed.

Overload the assignment operator in C++ [duplicate]

This question already has answers here:
C++ assignment operator - compiler generated or custom?
(5 answers)
Closed 7 years ago.
I have a general question i C++ that I coudln't get a clear answer to and I'm hoping I get an answer to it here. As a programmer, do I need to explicitly overload the assignment operator for a class when all instance data fields in this class are standard built in C++ data types.
No. You only need to overload the defaults if you are doing manual resource management. If all of your members can be treated as POD types then you will be fine.
No, you don't.​​​​​​​​​​​​​​​​​​​​​

finding addressof a object whose ampersand operator is overloaded and private [duplicate]

This question already has answers here:
If an operator is overloaded for a C++ class how could I use a default operator instead?
(3 answers)
Closed 8 years ago.
I have a class which is overloading ampersand(&) operator and made it private. I don't have a latest C++11 compliant compiler so is there any way by which I can get address of my object using current C++ compiler only
Thanks
reinterpret_cast<T *>(&reinterpret_cast<char&>(obj))
Dunno if it's safe though.. (well clearly it's a bit dodgy)
Use the addressof() function from Boost.Utility. If you don't want to use Boost, you can still look at it's implementation which consists of just a single header.