0
你会不会公开这样的私人类(请参阅函数get_module)?我认为它违反了一些OO封装,你认为如何?通过getter函数公开私人类
class Module
{
public:
virtual ~Module()
{}
virtual void do_me()
{
}
// lots of more code...
};
class MyHandler
{
private:
friend class Module;
class ModuleImpl : public Module
{
public:
~ModuleImpl()
{}
void do_me() override
{}
};
ModuleImpl m_moduleImpl;
public:
Module & get_module()
{
return m_moduleImpl;
}
// lots and lots of other stuff down here...
};
任何意见,将不胜感激。
也许'Module'只是一个抽象接口,实际的实现('ModuleImpl')仍然隐藏。 – user463035818
如果我的猜测是正确的,像这样做会阻止你编写包装函数来访问实现的每个单一方法 – user463035818
顺便说一句,只是因为有一个叫封装的原则并不意味着一切都必须是私有的。即使是类中的公共成员变量在某些情况下也是有意义的。 – user463035818