我宣告了一個模板專業化template <> class Component<NullType, NullType, NullType, NullType>,并定義了它。
我的問題是當我減少
NullTypein Component 時,p->Initialize()總是會成功并被呼叫。這是什么功能?另一個問題是,為什么我不能同時定義
bool Component<NullType, NullType, NullType>::Initialize(),并bool Component<NullType, NullType, NullType, NullType>::Initialize()在同一時間?
#include <iostream>
using namespace std;
class NullType {};
template <typename M0 = NullType, typename M1 = NullType,
typename M2 = NullType, typename M3 = NullType>
class Component {
public:
bool Initialize();
};
template <>
class Component<NullType, NullType, NullType, NullType> {
public:
bool Initialize();
};
bool Component<NullType, NullType, NullType>::Initialize() {
cout<<"Hello World3";
return true;
}
// bool Component<NullType, NullType, NullType, NullType>::Initialize() {
// cout<<"Hello World4";
// return true;
// }
int main()
{
auto p = new Component<>();
p->Initialize();
return 0;
}
uj5u.com熱心網友回復:
我的問題是當我減少
NullTypein Component
然后NullType將使用主模板中指定的默認引數,即。作為效果,
bool Component<NullType, NullType, NullType>::Initialize() {
與以下相同:
bool Component<NullType, NullType, NullType, NullType>::Initialize() {
另一個問題是,為什么我不能同時定義
bool Component<NullType, NullType, NullType>::Initialize(),并bool Component<NullType, NullType, NullType, NullType>::Initialize()在同一時間?
如上所述,它們被認為是相同的,您將收到重新定義錯誤。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/354089.html
下一篇:帶有模板的Yeoman復制目錄
