在这个关于 Curiously Recurring Template Patten 的网页中,派生类可以在堆栈上实例化(对象计数器示例,其中基类模板具有受保护的析构函数):CRTP-wiki ..我自己编译。
template <typename T>
struct counter
{
static int objects_created;
static int objects_alive;
counter()
{
++objects_created;
++objects_alive;
}
counter(const counter&)
{
++objects_created;
++objects_alive;
}
protected:
~counter() // objects should never be removed through pointers of this type
{
--objects_alive;
}
};
template <typename T> int counter<T>::objects_created( 0 );
template <typename T> int counter<T>::objects_alive( 0 );
class X : counter<X>
{
// ...
};
class Y : counter<Y>
{
// ...
};
但是这个答案说使基类的析构函数受保护将禁止在堆栈上实例化派生类:答案:
正如已经回答的那样,Poco::RefCountedObject 具有受保护的析构函数,因此不能在堆栈上创建从它继承的所有类......
所以,
(1)这个答案错了吗?还是我误会了?
(2)为什么CRTP的例子让析构函数受保护?是否意味着禁止在堆栈上实例化基类模板的特化?我可以在堆上实例化基类模板的特化吗(我试过了,我不能,但不知道为什么)?
提前致谢!