1

In C++, I'm trying to have a member variable hold reference to its owner object. This means that I must use *this before constructor body. This compiles in g++ -std=c++11. However, is this standard?

class Owner
{
    class Data
    {
        friend class Owner; 

        Owner& owner; 
        Data(Owner& owner) : owner(owner) {}
    } data;

public: 
    Owner() : data(*this) {}
};

In my specific use, data is a public field, providing access to some functionality of the owner (syntactic sugar as for(element : container))

Yuri Feldman
  • 2,354
  • 1
  • 20
  • 23

1 Answers1

1

Yes, it is standard. Your code is correct.

navyblue
  • 776
  • 4
  • 8