c++ - copy constructor and the initializer constructor in container and Array_C/C++_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > C/C++ > c++ - copy constructor and the initializer constructor in container and Array

c++ - copy constructor and the initializer constructor in container and Array

 2012/9/8 11:51:02  joe.bq.wang  程序员俱乐部  我要评论(0)
  • 摘要:weknowyoucandefineconstructorandthecopyconstructorforauserdefinedclass,andifyoufailetoprovideone,thecompilerwillbeabletogenerateoneforyou.Thegeneratedonesarecalleddefaultconstructorordefaultcopyconstructor
  • 标签:c++ CTO

we know you can define constructor and the copy constructor for a user defined class, and if you faile to provide one, the compiler will be able to generate one for you. ?The generated ones are called default constructor or default copy constructor, the former will call the default constructor for its member and its base classes, and the later will call the default memberwise copy constructor for its member if the member failes to provide one or the copy constructor if it has provided one, and its applies the same as regard to the base classes object parts.

?

but how it works if we are create a array of user define classes or a container like vector of user defined classes?

?

let's see an example.

?

#include "stdafx.h"

#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

using std::string;
using std::cout;
using std::endl;
using std::vector;
using std::copy;
using std::inserter;


class CustomClass 
{
public: 
	CustomClass(string name_, int age_) : name(name_), age(age_) {
	  cout << "inside CustomClass(string name, int age)" << endl;
	}
	CustomClass(const CustomClass& rhs) : name(rhs.name), age(rhs.age) {
	  cout << "inside CustomClass(const CustomClass& rhs)" << endl;
	}
private:
	std::string name;
	int age;
};

void TestVectorCopy()
{
	//vector<CustomClass> customClasses(4) = 
	// the results shows that it will first call the constructor with (string, int), then with the const CustomClass& - the copy constructor
	CustomClass customClasses[] = { CustomClass("joe", 29), CustomClass("Yun", 30), CustomClass("Cliff", 31), CustomClass("Nora", 31), CustomClass("Tom", 30) }; 

	vector<CustomClass> v_customClasses;

	v_customClasses.reserve(sizeof(customClasses) / sizeof(CustomClass));

	// as you can see, there are in total const CustomClasses&  called when copy from the CustomClass[] to the vector<CustomClass>
	copy(customClasses, customClasses + 4, inserter(v_customClasses, v_customClasses.begin())); 

	// as you will find in the following code, another CustomClasses is called
	vector<CustomClass> v_customClasses2 = v_customClasses;

	vector<CustomClass> v_customClasses3;
	v_customClasses.reserve(v_customClasses2.size());
	copy(v_customClasses.cbegin(), v_customClasses.cend(), inserter(v_customClasses3, v_customClasses3.begin()));

}

?

the result shows that copy constructor is called for each element of container, and the constructor is called for each of the initializer value of the first array.

发表评论
用户名: 匿名