Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create and use a class arrow operator?

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?

like image 908
Codesmith Avatar asked Feb 08 '11 01:02

Codesmith


People also ask

How do you use the arrow operator?

An Arrow operator in C/C++ allows to access elements in Structures and Unions. It is used with a pointer variable pointing to a structure or union. The arrow operator is formed by using a minus sign, followed by the greater than symbol as shown below.

How do you write an arrow operator?

The (->) arrow operator The -> is called the arrow operator. It is formed by using the minus sign followed by a greater than sign. Simply saying: To access members of a structure, use the dot operator.

What does -> mean in C programming?

The dot ( . ) operator is used to access a member of a struct, while the arrow operator ( -> ) in C is used to access a member of a struct which is referenced by the pointer in question.


2 Answers

The operator -> is used to overload member access. A small example:

#include <iostream> struct A  {     void foo() {std::cout << "Hi" << std::endl;} };  struct B  {     A a;     A* operator->() {         return &a;     } };  int main() {     B b;     b->foo(); } 

This outputs:

Hi 
like image 81
gr0v3r Avatar answered Sep 22 '22 21:09

gr0v3r


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, not operator*, so you could have the following class:

class PointerToString {     string a;  public:     class PtPtS     {     public:         PtPtS(PointerToString &s) : r(s) {}         string* operator->()         {             std::cout << "indirect arrow\n";             return &*r;         }     private:         PointerToString & r;     };      PointerToString(const string &s) : a(s) {}     PtPtS operator->()     {         std::cout << "arrow dereference\n";         return *this;     }     string &operator*()     {         std::cout << "dereference\n";         return a;     } }; 

Use it like:

PointerToString ptr(string("hello")); string::size_type size = ptr->size(); 

which is converted by the compiler into:

string::size_type size = (*ptr.operator->().operator->()).size(); 

(with as many .operator->() as necessary to return a real pointer) and should output

arrow dereference indirect dereference dereference 

Note, however, that you can do the following:

PointerToString::PtPtS ptr2 = ptr.operator->(); 

run online: https://wandbox.org/permlink/Is5kPamEMUCA9nvE

From Stroupstrup:

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. However, there is no new syntax introduced, so a member name is still required after the ->

like image 42
Daniel Gallagher Avatar answered Sep 24 '22 21:09

Daniel Gallagher