纯虚方法的模板特化
Template specialization of pure virtual method
期望此代码能够编译并工作
template<class T>
class Base
{
virtual void method() = 0;
};
template<>
void Base<int>::method() { std::cout << "overrided" << std::endl; }
Base<int> base;
但它给出了 'Base<int>': cannot instantiate abstract class
错误。认为部分专业化会使 Base<int>
非抽象并允许对其进行实例化。
有没有像这个一样短的工作解决方案,并且保持 Base
class 抽象?否则我可以使 Base
class 非抽象或使用 Nicol Bolas 的解决方案从这里:Template specialization and inheritance
整个class(而不是只有一个成员函数)的专业化如何:
template<class T>
struct TempClass
{
virtual void f() = 0;
};
template <>
struct TempClass<int>
{
virtual void f()
{
//...
}
};
注意TempClass<int>
不再是抽象class,但其他Base
class仍然是抽象classes, (TempClass<float>
, TempClass<double>
, TempClass<SomeClassType>
, ...
).
和
它不会包含通用 class TempClass
包含的字段。您将不得不从通用 Base 或 复制粘贴它们,这是更聪明的解决方案,
您将创建基础 class,其中包含两个专业都有的字段,然后使这些模板 class 继承自该基础 class:
template <typename T>
struct Base
{
// some members that all Base classes have
};
template <typename T>
struct TempClass: Base<T>
{
virtual void f() = 0;
};
template <>
struct TempClass<int>: Base<int>
{
virtual void f()
{
//...
}
};
这样就不需要丑陋的复制粘贴了。
如果它不适用于非模板 class,为什么它适用于模板 class?
#include<iostream>
class Base
{
virtual void method() = 0;
};
void Base::method() { std::cout << "overrided" << std::endl; }
Base base;
错误:
10 : error: cannot declare variable 'base' to be of abstract type 'Base'
Base base;
^
3 : note: because the following virtual functions are pure within 'Base':
class Base
^
8 : note: virtual void Base::method()
void Base::method() { std::cout << "overrided" << std::endl; }
^
Compilation failed
可以在 class 中提供纯虚函数的实现。这不会使 class 实例化。
class Base
{
virtual void method() = 0;
};
void Base::method() { /* Do something */ }
// This is still a problem since Base
// is still an abstract class, i.e. it is not still not
// instantiable.
Base base;
期望此代码能够编译并工作
template<class T>
class Base
{
virtual void method() = 0;
};
template<>
void Base<int>::method() { std::cout << "overrided" << std::endl; }
Base<int> base;
但它给出了 'Base<int>': cannot instantiate abstract class
错误。认为部分专业化会使 Base<int>
非抽象并允许对其进行实例化。
有没有像这个一样短的工作解决方案,并且保持 Base
class 抽象?否则我可以使 Base
class 非抽象或使用 Nicol Bolas 的解决方案从这里:Template specialization and inheritance
整个class(而不是只有一个成员函数)的专业化如何:
template<class T>
struct TempClass
{
virtual void f() = 0;
};
template <>
struct TempClass<int>
{
virtual void f()
{
//...
}
};
注意TempClass<int>
不再是抽象class,但其他Base
class仍然是抽象classes, (TempClass<float>
, TempClass<double>
, TempClass<SomeClassType>
, ...
).
和
它不会包含通用 class TempClass
包含的字段。您将不得不从通用 Base 或 复制粘贴它们,这是更聪明的解决方案,
您将创建基础 class,其中包含两个专业都有的字段,然后使这些模板 class 继承自该基础 class:
template <typename T>
struct Base
{
// some members that all Base classes have
};
template <typename T>
struct TempClass: Base<T>
{
virtual void f() = 0;
};
template <>
struct TempClass<int>: Base<int>
{
virtual void f()
{
//...
}
};
这样就不需要丑陋的复制粘贴了。
如果它不适用于非模板 class,为什么它适用于模板 class?
#include<iostream>
class Base
{
virtual void method() = 0;
};
void Base::method() { std::cout << "overrided" << std::endl; }
Base base;
错误:
10 : error: cannot declare variable 'base' to be of abstract type 'Base'
Base base;
^
3 : note: because the following virtual functions are pure within 'Base':
class Base
^
8 : note: virtual void Base::method()
void Base::method() { std::cout << "overrided" << std::endl; }
^
Compilation failed
可以在 class 中提供纯虚函数的实现。这不会使 class 实例化。
class Base
{
virtual void method() = 0;
};
void Base::method() { /* Do something */ }
// This is still a problem since Base
// is still an abstract class, i.e. it is not still not
// instantiable.
Base base;