【发布时间】:2018-09-20 22:12:37
【问题描述】:
我在使用 CRTP(奇怪的重复模板模式)时发现了这一点。
template <typename T>
class Base {
private:
void f() {
//when T has its own f(), it calls that
//when T doesn't have, it calls itself, which is invoked recursively
//but i expect the compiler to complain that f() is private when T doesn't have its own f()
static_cast<T*>(this)->f();
}
public:
void g() {
f();
}
};
class Derived : public Base<Derived> {};
我以为我理解public、protected 和private,但对于这种情况,我好像错了。任何解释表示赞赏!
【问题讨论】: