https://blog.csdn.net/veryday_code/article/details/7982996?utm_source=blogxgwz7
https://blog.csdn.net/qq_16993255/article/details/78652960
以下几种情况会调用拷贝构造函数
1. 以一个对象初始化另一个对象
2. 函数以某个对象为参数
3. 函数返回某个对象
4. 初始化序列式容器的元素
2.
void test2()
{
Test t1(1,2);
Test t2;
t2=t1;//调用的不是拷贝构造函数,调用的是=号操作符,也能够完成将t1的值给t2,但是不是调用t2的拷贝构造函数
}
另外以下程序 points[i] = pointsArray1.points[i]; 这一行并不会触发 拷贝构造函数。调用的是 = 赋值运算符。赋值运算符完成两个对象的复制。
//6_21.cpp
#include <iostream>
#include <cassert>
#include <vector>
#include <map>
using namespace std;
class Point {
public:
Point() : x(0), y(0) {
cout<<"Point Default Constructor called."<<endl;
}
Point(int x, int y) : x(x), y(y) {
cout<< "Point Constructor with two arguments called."<<endl;
}
Point(const Point & point){
x= point.x;
y = point.y;
cout<< "Point copy Constructor called."<<endl;
}
~Point() { cout<<"Point Destructor called."<<endl; }
int getX() const { return x; }
int getY() const { return y; }
void move(int newX, int newY) {
x = newX;
y = newY;
}
//private:
int x, y;
};
//动态数组类
class ArrayOfPoints {
public:
ArrayOfPoints(int size) : size(size) {
points = new Point[size];
}
~ArrayOfPoints() {
cout << "Deleting..." << endl;
delete[] points;
}
//获得下标为index的数组元素
Point &element(int index) {
assert(index >= 0 && index < size); //如果数组下标不会越界,程序中止
return points[index];
}
ArrayOfPoints(const ArrayOfPoints & pointsArray1) {
points = new Point[pointsArray1.size];
for(int i=0; i< pointsArray1.size; i++){
printf( "copy arrayobj element -----points[%d]: x= %d y=%d \n",i, points[i].x,points[i].y);
points[i] = pointsArray1.points[i];
}
}
private:
Point *points; //指向动态数组首地址
int size; //数组大小
};
struct A {
int i,j;
A(int m,int n):i(m),j(n) { }
};
// A func(int m,int n ) { return {m,n}};
Point g(){
Point A(1,2);
return A;
}
int main() {
g();
int count;
cout << "Please enter the count of points: ";
cin >> count;
ArrayOfPoints pointsArray1(count); //创建对象数组
#if 0
int arr[3]{1,2,3};
int i = 0;
for(auto element: arr){
cout << "arr[" << i++ << "]: "<<element<<endl;
}
i=0;
vector<int> iv{4, 5, 6};
for(auto element: iv){
cout << "iv[" << i++ << "]: "<<element<<endl;
}
i=0;
map<int, string> mp{{1, "a"}, {2, "b"}};
// for(; i< mp.size();i++){
// cout << "mp[" << i++ << "]: "<<mp.get(i)<<endl;
// }
for (auto x: mp) {
std::cout << x.first << ": " << x.second << '\n';
}
i=0;
string str{"Hello World"};
for(auto element: str){
cout << "str[" << i++ << "]: "<<element<<endl;
}
i=0;
// int * p = new int[20]{1,2,3};
// for(auto element: p){
// cout << "p[" << i++ << "]: "<<element<<endl;
// }
// i=0;
A * pa = new A {3,7};
// pointsArray1.element(0).move(5,10);
// pointsArray1.element(1).move(15,20);
#endif
ArrayOfPoints pointsArray2 = pointsArray1; //创建对象数组副本
cout<< "-------"<<endl;
Point a1(2,3);
Point * a2= new Point[2];
cout<<"a2= "<<a2<<endl;
a2 = &a1;
cout<<"a2="<<a2<<endl;
a2[1] = a1;
cout<<"--------"<<endl;
// cout << "Copy of pointsArray1:" << endl;
// cout << "Point_0 of array2: " << pointsArray2.element(0).getX() << ", "
// << pointsArray2.element(0).getY() << endl;
// cout << "Point_1 of array2: " << pointsArray2.element(1).getX() << ", "
// << pointsArray2.element(1).getY() << endl;
// pointsArray1.element(0).move(25, 30);
// pointsArray1.element(1).move(35, 40);
// cout << "After the moving of pointsArray1:" << endl;
// cout << "Point_0 of array2: " << pointsArray2.element(0).getX() << ", "
// << pointsArray2.element(0).getY() << endl;
// cout << "Point_1 of array2: " << pointsArray2.element(1).getX() << ", "
// << pointsArray2.element(1).getY() << endl;
return 0;
}