【发布时间】:2018-11-22 22:11:39
【问题描述】:
我的代码:
class Controller {
private:
class ControllerMetals {
private:
int m_size;
Metals * m_metals;
public:
ControllerMetals();
Metals & getMetals() const;
int getSize() const;
void setSize(int size) { m_size = size; }
void init();
void show();
void erase();
friend void Controller::start(ControllerMetals & c); // why don't work ?
};
private:
ControllerMetals * controlMetals;
public:
void start(ControllerMetals & c);
ControllerMetals * getControlMetals() const;
Controller();
};
我想让一个 void 开始访问 ControllerMetals 类中的私有成员。为什么朋友声明不起作用?
【问题讨论】:
-
既然 ControllerMetals 在 Controller 中是私有的,为什么不在 ControllerMetas 中公开所有内容呢?显然嵌套的私有类与外部类是紧密耦合的。
-
我有一个任务,我必须将我的内部类声明为外部类的私有成员。并且所有成员都必须是私有的。
-
Controller::start在您加为好友时不存在。当您与它成为朋友时,您可以声明数据类型,但我认为您不能使用函数。转发声明ControllerMetals,以便您可以引用它,然后在函数之后正式定义它。为什么我不直接回答?