概要
ポインターまたは参照である可能性があるテンプレート化された型でクラス メソッドを呼び出す方法はありますか?
詳細
次の重要なユーザー定義型のいずれかを取ることができるテンプレート化された QuadTree 実装があります。
//Abstract Base Class
a2de::Shape
//Derived Classes
a2de::Point
a2de::Line
a2de::Rectangle
a2de::Circle
a2de::Ellipse
a2de::Triangle
a2de::Arc
a2de::Spline
a2de::Sector
a2de::Polygon
しかし、それらはすべて a2de::Shape から派生しているため、ポインターまたは参照である可能性があります。したがって、特殊化は次のように宣言されます。
template class QuadTree<a2de::Shape&>;
//...similar for all derived types as references.
template class QuadTree<a2de::Shape*>;
//...similar for all derived types as pointers
私が抱えている問題は、間接化 (またはその欠如) が不明なときにクラスメソッドを呼び出す機能であり、テンプレートのために両方のコードセットが生成されます。
template<typename T>
bool QuadTree<T>::Add(T& elem) {
//When elem of type T is expecting a pointer here
//-> notation fails to compile where T is a reference i.e.:
//template class QuadTree<a2de::Shape&>
//with "pointer to reference is illegal"
if(elem->Intersects(_bounds) == false) return false;
//...
}
を使用するように上記の行を変更すると、. (ドット)表記:
template<typename T>
bool QuadTree<T>::Add(T& elem) {
//When elem of type T is expecting a reference here
//. (dot) notation fails to compile where T is a pointer i.e.:
//template class QuadTree<a2de::Shape*>
//with "pointer to reference is illegal"
if(elem.Intersects(_bounds) == false) return false;
//...
}
ポインターベースの型を優先して参照ベースの型を削除すると (Quadtree クラスの宣言と使用を含む)、エラーが発生しますleft of .<function-name> must have class/struct/union
。
参照ベースの型 (Quadtree クラスの宣言と使用を含む) を優先してポインタ ベースの型を削除すると、前述の状態reference to pointer is illegal
が再び発生します。
コンパイラ: VS2010-SP1