c++ - Inheriting vector class with different types -
i trying inherit stl vector class create new class on own. use vector's base properties add new functions sum or divide. problem here trying implement class different types. how can use both inheritance , template @ same time ?
below class body need help.
//template <class t> ? class newvector<t> : public vector<t>{ //some constructors };
yes. inheriting stl not idea. aware of it. lets using inheritance.
the problem face here building constructors.
i calling constructor in main this,
newvector<int> v1(3);
but problem can not create v1 vector stl vector does. when try debug 0 size , 0 capacity. how edit size , capacity in constructor ? tried 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 size of v1 vector 0, want see 3 mentioned. little nice.
here main file. know need write constructors need work both int , double , how tell class them ?
using namespace std; int main() { newvector<int> v1(3); // creating objects newvector<int> v2(3); // vector elements assigned randomly 0 10 newvector<double> v3(5); newvector<double> v4(5); .....
first of all, publicly inheriting std::vector
, other standard library containers is considered bad idea.
keeping in mind, syntax need class template inherits class template is
template <class t> class newvector : public vector<t> { // .... };
Comments
Post a Comment