How do I create and use a class boom operator?

advertisements

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 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