来自维基百科
template <class T>
struct Base
{
void interface()
{
// ...
static_cast<T*>(this)->implementation();
// ...
}
static void static_func()
{
// ...
T::static_sub_func();
// ...
}
};
struct Derived : Base<Derived>
{
void implementation();
static void static_sub_func();
};看来我们将只在implementation()函数中使用interface()。那么,我们为什么不将implementation()声明为private函数呢?
发布于 2015-05-04 05:47:25
如果将implementation()设置为私有,则基类将无法访问它。要使它能够访问它,您必须使基成为派生类的朋友:
struct Derived : Base<Derived>
{
static void static_sub_func();
private:
friend class Base<Derived>; //make base friend
void implementation();
};希望这能有所帮助。
https://stackoverflow.com/questions/30023249
复制相似问题