1

I am trying to inherit STL vector class to create a new class on my own. It will use vector's base properties but i will add some new functions like sum or divide. The problem here is i am trying to implement that class also for different types. How can i use both inheritance and template at the same time ?

Below is my class body where i need some help.

//template <class T> 
?
class NewVector<T> : public vector<T>{

//some constructors
};

Yes. Inheriting STL is not a good idea. I am aware of it. But lets say i am using this inheritance.

The problem i face here is building constructors.

I am calling constructor in main like this,

NewVector<int> v1(3);

but the problem is i can not create this v1 vector like STL vector does. When i try to debug get 0 for size and 0 for capacity. How will i edit the size and capacity in constructor ? I tried to do those but couldn't manage.

template <class T> 
class NewVector : public vector<T>{
public:

NewVector(T n){
const ArithmeticVector<T> &v1(n);
cout<<v1.size()<<endl;

    };
//some constructors
};

i see the size of v1 vector 0, but i want to see it 3 as i mentioned. A little help would be nice.

Here is the main file. i know i need to write some constructors but they need to work both int and double and also how will i tell my class to do them ?

using namespace std;
int main() {

NewVector<int> v1(3); // creating some objects
NewVector<int> v2(3); // vector elements are assigned randomly from 0 to 10
NewVector<double> v3(5);
NewVector<double> v4(5);

.....
erenkabakci
  • 442
  • 4
  • 15

2 Answers2

3

Please don't do this! I wouldn't like to have to pick up the code you've written and maintain it. If I see a std::vector than I know what I'm dealing with immediately. With your class I wouldn't.

Note that std::vector does not have a virtual destructor and someone will, one day, use a std::vector pointer to refer to your class.

I'd consider developing the operations you need along the lines of the algorithms approach that stl takes. This has the added bonus that your functions have wider usage; across many stl containers.

If you don't like the actual way that std::vector actually stores the data then build your own class from scratch.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
2

First of all, publicly inheriting from std::vector and other standard library containers is considered a bad idea.

Keeping that in mind, the syntax you need for a class template that inherits from another class template is

template <class T> 
class NewVector : public vector<T>
{
// ....
};
Community
  • 1
  • 1
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • Thank you for help. It was a good start for me. But the point i dont understand here is how will i define constructors for T type ? Will i be able to use STL class' methods together with mine ? `NewVector(T n) {// bla bla};` Will this constructor work for `NewVector v1(3);` and `NewVector v4(5);` ?? – erenkabakci May 19 '13 at 10:31
  • @user2374877 I don't understand your question. `T` is a template parameter. It either has user defined constructors or it doesn't. – juanchopanza May 19 '13 at 10:33