まず、投稿したコードは、次の行のためにコンパイルできません。
public:
vector<T> vector; //maybe i should not initalize this
次のエラーが表示されます。
declaration of ‘std::vector<T, std::allocator<_Tp1> > ArithmeticVector<T>::vector’
/usr/include/c++/4.4/bits/stl_vector.h:171: error: changes meaning of ‘vector’ from ‘class std::vector<T, std::allocator<_Tp1> >’
名前「ベクトル」を表示するクラス テンプレート宣言の上に std 名前空間全体を導入し、それを使用してオブジェクトを宣言するためです。それは「double double;」と書くようなものです。
私が欲しいのは、STLベクター型と同じようにv9ベクターまたはv1ベクターを作成することです。
これが必要な場合は、次のコードを実行します。
#include <vector>
#include <memory>
template
<
class Type
>
class ArithmeticVector
:
public std::vector<Type, std::allocator<Type> >
{
public:
ArithmeticVector()
:
std::vector<Type>()
{}
// Your constructor takes Type for an argument here, which is wrong:
// any type T that is not convertible to std::vector<Type>::size_type
// will fail at this point in your code; ArithmeticVector (T n)
ArithmeticVector(typename std::vector<Type>::size_type t)
:
std::vector<Type>(t)
{}
template<typename Iterator>
ArithmeticVector(Iterator begin, Iterator end)
:
std::vector<Type>(begin, end)
{}
};
int main(int argc, const char *argv[])
{
ArithmeticVector<double> aVec (3);
return 0;
}
STL で定義されているアルゴリズム (accumulate など) とは異なるベクトルの算術演算に関心がある場合は、ベクトル クラスに集中してメンバー関数を追加するのではなく、特定のベクトルの概念を期待するベクトルの汎用アルゴリズムを作成することを検討できます。代わります。そうすれば、継承についてまったく考える必要がなくなり、一般的なアルゴリズムはベクトルのさまざまな概念で機能します。