开发者

Returning reference to class with overloaded private & operator?

开发者 https://www.devze.com 2022-12-19 01:07 出处:网络
I got a class called Property (from external library == cannot be modified) that has private overloaded & operator. I use this class in another class as a property and (for sanity reasons) I\'d li

I got a class called Property (from external library == cannot be modified) that has private overloaded & operator. I use this class in another class as a property and (for sanity reasons) I'd like to return a reference to this property through the Get method. However I got the 'cannot access private member declared in class' error I cannot handle. Is there a way to walk around it - without making the Property public public.

// Some external class.
class Property
{
    Property*   operator&() const;
};

class MyClass
{
protected:
    Property m_Property;

public:

    // error C2248: 'Property::operator &' : cannot access private member de开发者_高级运维clared in class 'Property'
    const Property& GetProperty() const
    {
        return *& this->m_Property;
    }
};


I may be missing something, but why not simply say:

const Property& GetProperty() const
{
  return this->m_Property;
}

The fact that the operator& is private pretty clearly indicates that you are not supposed to call it.

0

精彩评论

暂无评论...
验证码 换一张
取 消