在C++中,我有一个A类,它是B类的朋友.
我看起来像继承的B类不是A类的朋友.
我这是C++的限制还是我的错误?
这是一个例子.编译时,我收到一行"返回新纪念品"的错误:
纪念品::纪念品:无法访问纪念品中宣布的私人会员.
class Originator;
class Memento
{
friend class Originator;
Memento() {};
int m_Data;
public:
~Memento() {};
};
class Originator
{
public:
virtual Memento* createMemento() = 0;
};
class FooOriginator : public Originator
{
public:
Memento* createMemento()
{
return new Memento; // Impossible to access private member of Memento
}
};
void main()
{
FooOriginator MyOriginator;
MyOriginator.createMemento();
}
Run Code Online (Sandbox Code Playgroud)
我当然可以添加FooOriginator作为Memento的朋友,但是,这意味着我必须添加所有Originator继承的类作为Memento的朋友,这是我想要避免的.
任何的想法 ?
请参阅:C++中的朋友范围
投票完全相同.
I looks like inherited classes of B are not friend of class A.
Run Code Online (Sandbox Code Playgroud)
正确
I this a limitation of C++ or my mistake ?
Run Code Online (Sandbox Code Playgroud)
这是C++的工作方式.我不认为它是一种限制.
归档时间: |
|
查看次数: |
14076 次 |
最近记录: |