关于类模板的构造函数调用问题
自己照着算法书上写了一个类模板Vector来仿造vector的功能,其中的构造函数如下
explicit Vector( int initSize = 0 ): theSize(initSize), theCapacity(initSize + SPARE_CAPACITY) { objects = new Object[theCapacity]; } Vector(const Vector & rhs): objects()//书中的程序括号中有NULL { operator=(rhs); } const Vector & operator= (const Vector & rhs) { if(this != &rhs) { delete[] objects; theSize = rhs.size(); theCapacity = rhs.capacity(); objects = new Object[capacity()]; for(int k = 0; k < size(); k ++) objects[k] = rhs.objects[k]; } return *this; }
int main(){ Vector<string> vect(3); Vector<string> v1(vect);// Vector<string> v2=vect; --->wrong Vector<string> v2; v2 = vect; //size()和capacity()函数分别返回vector的大小和容量,这两个函数是正确的 cout<<"The size of the initial vect: "<<vect.size()<<endl; cout<<"The capacity of the initial vect: "<<vect.capacity()<<endl; cout<<"The size of the initial v1: "<<v1.size()<<endl; cout<<"The capacity of the initial v1: "<<v1.capacity()<<endl; cout<<"The size of the initial v2: "<<v2.size()<<endl; cout<<"The capacity of the initial v2: "<<v2.capacity()<<endl; return 0;}