2016-08-04 8 views
4

私は内部クラスを定義するクラスArrayconst_iterator内部クラスのメソッド定義

template <class T, int SIZE = 100> 
class Array 
{ 
    // my class here 
public: 
    class const_iterator 
    { 
    // my class here 
    }; 


    void insert(const_iterator position, int value); 
}; 


template <class T, int SIZE /*= 100*/> 
void Array<T, SIZE>::insert(const_iterator position, int value) 
{ 
    // impl 
} 

それは、通常のI関数を定義しており、最初の引数としてconst_iterator positionを使用しているクラスの外で持ってタイプtypename Array<T, SIZE>::const_iterator positionの代わりに入力しますか?この規格は準拠していますか? Array以外のクラスconst_iteratorがある場合はどうなりますか?

+1

あなたが示唆しているように、これはテンプレートとは何の関係もなく、同じルールがテンプレート化されていないクラスにも適用されます。 – Holt

答えて

4

はい、それは完全に罰金と標準だ(まあ、クラス内であなたのinsert()メンバ関数は、その型のパラメータを取る持つ前最初const_iteratorを宣言する必要があります)。

クラス外のメンバー関数定義の場合、クラス名スコープ導入者の後に来るすべての名前は、そのクラスのスコープ内で最初に検索されます。 Arrayの別のconst_iterator外があった場合には、我々が探し始める範囲だからだから、我々はまだ第一の内側ものを見つけるだろう、それは、この特別な参照を持っている後のクラスのイントロだけでそれらの名前だということに注意してください:。

// this is okay 
template <class T, int SIZE> 
typename Array<T, SIZE>::const_iterator Array<T, SIZE>::some_method() { ... } 

// error: this does *not* find the nested const_iterator class 
template <class T, int SIZE> 
const_iterator Array<T, SIZE>::some_method() { ... } 

// this is okay. trailing return type comes after the class introducer 
template <class T, int SIZE> 
auto Array<T, SIZE>::some_method() 
    -> const_iterator 
{ ... } 
+0

編集した質問が正しいものに変更されました。ありがとう! :) –

関連する問題