开发者

Returning reference to class with overloaded private & operator?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜