私はテンプレート化された関数を持つ基本クラスを持っています。テンプレート化された関数は一般的なテンプレート化された型を持ちます。テンプレートクラス(特殊)を子クラスにオーバーロードするにはどうすればよいですか?
#ifndef BASE_CLASS
#define BASE_CLASS
#include <iostream>
using namespace std;
struct Type1
{
};
struct Type2
{
};
class baseClass
{
public:
template<class Type>
void doStuff(Type & t)
{
templateFunction(t);
}
template<class Type>
void templateFunction(Type & t);
};
template<class Type>
void baseClass::templateFunction(Type & t)
{
cout << "This is the generic function!" << endl;
}
template<>
void baseClass::templateFunction(Type1 & t)
{
cout << "This is the specialized function: - Type1" << endl;
}
#endif
また、「baseClass」を継承する子クラスもあります。ただし、子クラスはその特殊化のために異なる機能を必要とします。
#ifndef CHILD_CLASS
#define CHILD_CLASS
#include "BaseClass.h"
class ChildClass : public baseClass
{
public:
};
template<>
void ChildClass::templateFunction(Type1 & t)
{
cout << "We overloaded the specialized template function for type 1!" << endl;
}
#endif
上記コンパイルされない:
ChildClass.h:13:エラー:âtemplateFunctionâがâChildClassâ ChildClass.hで宣言されないメンバ関数:13:エラー:無効な関数宣言
場合私"オーバーロードされた" 機能を変更します。
template<>
void baseClass::templateFunction(Type1 & t)
{
cout << "We overloaded the specialized template function for type 1!" << endl;
}
私が手: ChildClass.h:13:エラー:避けるBAの再定義をseClass :: BaseClass.h A [タイプ=タイプ1付き] templateFunction(タイプ&):36:エラー:避ける基底クラス:: [タイプと=のType1] templateFunction(タイプ&)以前ここ
するにはどうすればよいです宣言â私は正しく、子クラスの特殊なテンプレート関数をオーバーロードしますか?参考のため
、メイン:の提案に
#include "BaseClass.h"
#include "ChildClass.h"
int main()
{
Type1 first;
Type2 second;
baseClass theBaseClass;
ChildClass theChildClass;
theBaseClass.doStuff(first);
theBaseClass.doStuff(second);
theChildClass.doStuff(first);
theChildClass.doStuff(second);
return 0;
}
:
#ifndef CHILD_CLASS
#define CHILD_CLASS
#include "BaseClass.h"
class ChildClass : public baseClass
{
public:
template<class Type>
void templateFunction(Type & t);
};
template<>
void ChildClass::templateFunction(Type1 & t)
{
cout << "We overloaded the specialized template function for type 1!" << endl;
}
#endif
出力:
This is the specialized function: - Type1
This is the generic function!
This is the specialized function: - Type1
This is the generic function!
Kerrek SBは、私はにChildClassを変更しました
私は望んでいた:
This is the specialized function: - Type1
This is the generic function!
We overloaded the specialized template function for type 1!
This is the generic function!
これはまだ動作しません。
テンプレート化機能を仮想することはできません、あなたが派生クラスのメンバ関数を再定義する意味がわかられる一方*隠れ*元の機能?ベースへのポインタを介して再定義された関数に到達することはできません!確かな場合は、非特化宣言を派生クラスに追加する必要があります。 –
私はbaseClassのオリジナルの特別な機能にアクセスする必要はありません。少なくともChildClassの任意のインスタンスに対して。 – Alex
その場合は、関数のプライマリテンプレート宣言を特殊化する前に派生クラスに追加すると、それを実行できるはずです。 –