gpt4 book ai didi

c++ - 返回对重载私有(private) & 运算符的类的引用?

转载 作者:行者123 更新时间:2023-11-28 03:58:28 25 4
gpt4 key购买 nike

我有一个名为 Property 的类(来自外部库 == 无法修改),它具有私有(private)重载 & 运算符。我在另一个类中将此类用作属性,并且(出于理智原因)我想通过 Get 方法返回对此属性的引用。但是我遇到了无法处理的“无法访问类中声明的私有(private)成员”错误。有没有办法绕过它 - 无需公开属性。

// 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;
}
};

最佳答案

我可能漏掉了什么,但为什么不直接说:

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

operator& 是私有(private)的这一事实清楚地表明您不应该调用它。

关于c++ - 返回对重载私有(private) & 运算符的类的引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2206729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com