返回对具有重载 private & 的类的引用操作员?

发布于 2024-08-20 00:58:05 字数 532 浏览 5 评论 0原文

我有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秉烛思 2024-08-27 00:58:05

我可能遗漏了一些东西,但为什么不简单地说:

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

事实上,operator& is private 非常清楚地表明您不应该调用它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文