返回对具有重载 private & 的类的引用操作员?
我有一个名为 Property 的类(来自外部库==无法修改),它具有私有重载&操作员。我在另一个类中使用此类作为属性,并且(出于理智原因)我想通过 Get 方法返回对此属性的引用。但是我收到了“无法访问类中声明的私有成员”错误,我无法处理。有没有办法绕过它 - 而不将财产公开。
// Some external class.
class Property
{
Property* operator&() const;
};
class MyClass
{
protected:
Property m_Property;
public:
// error C2248: 'Property::operator &' : cannot access private member declared in class 'Property'
const Property& GetProperty() const
{
return *& this->m_Property;
}
};
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 declared in class 'Property'
const Property& GetProperty() const
{
return *& this->m_Property;
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可能遗漏了一些东西,但为什么不简单地说:
事实上,operator& is private 非常清楚地表明您不应该调用它。
I may be missing something, but why not simply say:
The fact that the operator& is private pretty clearly indicates that you are not supposed to call it.