erenkabakci
erenkabakci

Reputation: 442

Using object inside constructor

I am creating my own vector class inheriting the STL one. I have a problem while creating the object.

Here is my class.

using namespace std;

template <class T> 
class ArithmeticVector : public vector<T>{
public:
    vector<T> vector; //maybe I should not initalize this
    ArithmeticVector(){};
    ArithmeticVector(T n) : vector(n){
   //something here
};

In main; I am calling this;

ArithmeticVector<double> v9(5);

or

ArithmeticVector<int> v1(3);

What I want is creating v9 vector or v1 vector just like STL vector type. But what I get is a vector inside my newly created object. I want my object to be a vector at first.

Maybe I should use that v1 object inside constructor ? Thanks for help.

Upvotes: 2

Views: 145

Answers (2)

rubenvb
rubenvb

Reputation: 76721

If you need elementwise operations and math on a std::vector, use std::valarray. If not, I don't see why you are subclassing std::vector.

Don't inherit form std:: containers, they don't have a virtual destructor and will blow up in your face if deleted from a pointer to base.

EDIT If you need to define operations on std::vector, you can by defining the operators outside of the class, and using its public interface.

Upvotes: 4

tmaric
tmaric

Reputation: 5477

First of all, the code that you posted cannot compile because of this line:

public:
    vector<T> vector; //maybe i should not initalize this

you should see this error:

 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> >’

because you are introducing the whole std namespace above the class template declaration which makes the name "vector" visible, and then you use it to declare an object. That's like writing "double double;".

What i want is creating v9 vector or v1 vector just like STL vector type.

If this is what you want, here is the code that does it:

#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;
}

If you are interested in arithmetic operations on vectors which are different than the algorithms defined in STL (accumulate, etc), instead of concentrating on the vector class and adding member functions, you can consider writing generic algorithms for vectors that expect specific vector Concepts in stead. Then you don't have to think about inheritance at all, and your generic algorithms can work on different Concepts of a vector.

Upvotes: 1

Related Questions