【发布时间】:2020-02-24 23:41:59
【问题描述】:
我正在浏览我当地的一个图书馆并注意到以下内容:
class Derived : public Base
{
friend class Base; // serves as a factory
protected:
pthread_spinlock_t allocLock;
bool isSafe{ true };
Derived();
char *funcA() override;
public:
void funcB( bool _in ) override;
virtual ~Derived();
};
我无法理解 Base 是否已被继承,那么为什么它已成为 friend 的 Derived。另外,评论充当工厂在这里有什么意义?
如果要访问Base函数,::操作符就足够了。
它与设计方法更相关吗?
【问题讨论】:
-
注释掉那一行,编译,看看有什么问题。
-
阅读 CRTP。奇怪地重复出现的模板模式。
-
@Unapiedra CRTP 是派生类是基类的模板参数。当基类是派生类的朋友时不是这样。听起来很相似,但实际上非常不同。
标签: c++ c++11 inheritance friend