编程语言
首页 > 编程语言> > C++ 17 翁恺> 拷贝构造

C++ 17 翁恺> 拷贝构造

作者:互联网

#include<iostream>
#include<string>
using namespace std;

static int objectCount = 0;

class HowMany
{
public:
	HowMany() { objectCount++; print("HowMany()"); }
	void print(const string& msg="")
	{
		if (msg.size() != 0)cout << msg << ":";
		cout << "objectCount = " << objectCount << endl;
	}
	~HowMany() { objectCount--; print("~HowMany()"); }
};


HowMany f(HowMany x)
{
	cout << "begin of f()" << endl;
	x.print("x argument inside f()");
	cout << "end of f()" << endl;
	return x;
}

int main()
{
	HowMany h;
	h.print("after construction of h");
	HowMany h2 = f(h);//没调用默认构造函数,但f()运行完调用了一次析构,所以f()运行时一定也调用了默认构造函数,只是我们没看到
	//h2的创建也绕过了默认构造函数
	h.print("after call to f()");
	return 0;
}

 

#include<iostream>
#include<string>
using namespace std;

static int objectCount = 0;

class HowMany
{
public:
	HowMany() { objectCount++; print("HowMany()"); }
	void print(const string& msg="")
	{
		if (msg.size() != 0)cout << msg << ":";
		cout << "objectCount = " << objectCount << endl;
	}
	~HowMany() { objectCount--; print("~HowMany()"); }
};


HowMany f(HowMany x)
{
	cout << "begin of f()" << endl;
	x.print("x argument inside f()");
	cout << "end of f()" << endl;
	return x;
}

int main()
{
	HowMany h;
	h.print("after construction of h");
	//HowMany h2 = f(h);//没调用默认构造函数,但f()运行完调用了一次析构,所以f()运行时一定也调用了默认构造函数,只是我们没看到
	//h2的创建也绕过了默认构造函数

	HowMany h3 = h;//用h构造出一个新的h3,但是构造时没有经过默认构造函数

	h.print("after call to f()");

	//最后又调用了两次析构
	return 0;
}

#include<iostream>
#include<string>
using namespace std;

static int objectCount = 0;

class HowMany
{
public:
	HowMany() { objectCount++; print("HowMany()"); }
	HowMany(int i) { objectCount++; print("HowMany(int)"); }
	void print(const string& msg="")
	{
		if (msg.size() != 0)cout << msg << ":";
		cout << "objectCount = " << objectCount << endl;
	}
	~HowMany() { objectCount--; print("~HowMany()"); }
};


HowMany f(HowMany x)
{
	cout << "begin of f()" << endl;
	x.print("x argument inside f()");
	cout << "end of f()" << endl;
	return x;
}

int main()
{
	HowMany h;
	h.print("after construction of h");
	HowMany h2 = 10;
    //HowMany h2(10);//同上,在c++中初始化对象用 () 或 = 是等价的
	//最后又调用了两次析构
	return 0;
}

#include<iostream>
#include<string>
using namespace std;

static int objectCount = 0;

class HowMany
{
public:
	HowMany() { objectCount++; print("HowMany()"); }
	HowMany(int i) { objectCount++; print("HowMany(int)"); }
	HowMany(const HowMany& o) { objectCount++; print("HowMany(HowMany)"); }
	void print(const string& msg="")
	{
		if (msg.size() != 0)cout << msg << ":";
		cout << "objectCount = " << objectCount << endl;
	}
	~HowMany() { objectCount--; print("~HowMany()"); }
};


HowMany f(HowMany x)
{
	cout << "begin of f()" << endl;
	x.print("x argument inside f()");
	cout << "end of f()" << endl;
	return x;
}

int main()
{
	HowMany h;
	h.print("after construction of h");
	HowMany h2 = h;
	//最后又调用了两次析构
	return 0;
}

 

#include<iostream>
#include<string>
using namespace std;

static int objectCount = 0;

class HowMany
{
public:
	HowMany() { objectCount++; print("HowMany()"); }
	HowMany(int i) { objectCount++; print("HowMany(int)"); }
	HowMany(const HowMany& o) { objectCount++; print("HowMany(HowMany)"); }
	void print(const string& msg="")
	{
		if (msg.size() != 0)cout << msg << ":";
		cout << "objectCount = " << objectCount << endl;
	}
	~HowMany() { objectCount--; print("~HowMany()"); }
};


HowMany f(HowMany x)
{
	cout << "begin of f()" << endl;
	x.print("x argument inside f()");
	cout << "end of f()" << endl;
	return x;
}

int main()
{
	HowMany h;
	h.print("after construction of h");
	HowMany h2 = f(h);
	//最后又调用了三次析构
	return 0;
}

HowManyconst HowMany& 叫做 拷贝构造 函数。

拷贝构造 函数:成员级别的拷贝(不是字节对字节的拷贝),成员里有其它类的成员,继续调用其它类的拷贝构造进行拷贝,没有显示给出就会调用默认的。

调用默认拷贝,如果有指针,两个指针指向同一块内存;如果有引用,两个引用绑定同一个变量

Copy pointer: 

#ifndef PERSON_H_
#define PERSON_H_
class Person
{
public:
	Person(const char* s);//构造函数
	~Person();//析构函数
	void print();//函数原型,没有实际body,有一些什么样的函数
	//...
//private:
	char* name ;//数据成员 char* instead of string
	//...
};
#endif // !PERSON_H_

//person.h

 

#include "person.h"
#include<iostream>
#include<string.h>
using namespace std;

//在.cpp文件中定义在.h文件中声明的那些东西的实体
Person::Person(const char* s)
{
	name = new char[::strlen(s)+1];
	::strcpy(name,s);
}
Person::~Person()
{
	//delete[] name;
}

void Person::print()
{
	cout << "something" << endl;
}
//person.cpp
#include<stdio.h>
#include "person.h"
using namespace std;

int main()
{
	Person p1("Jhon");
	Person p2 = p1;//Person p2(p1);
	printf("p1.name=%p\n", p1.name);
	printf("p2.name=%p\n", p2.name);
	return 0;
}
//main.cpp

指向同一块内存: 

p1 和 p2 指向同一块内存,即 Copy pointer,而我们想要 Copy entire block。 

 

 Copy entire block :

#ifndef PERSON_H_
#define PERSON_H_
class Person
{
public:
	Person(const char* s);//构造函数
	Person(const Person& w);//拷贝构造函数
	~Person();//析构函数
	void print();//函数原型,没有实际body,有一些什么样的函数
	//...
//private:
	char* name ;//数据成员 char* instead of string
	//...
};
#endif // !PERSON_H_
//person.h
#include "person.h"
#include<iostream>
#include<string.h>
using namespace std;

//在.cpp文件中定义在.h文件中声明的那些东西的实体
Person::Person(const char* s)
{
	name = new char[::strlen(s)+1];
	::strcpy(name,s);
}
Person::Person(const Person& w)
{
	name = new char[::strlen(w.name) + 1];
	::strcpy(name, w.name);
}
Person::~Person()
{
	//delete[] name;
}

void Person::print()
{
	cout << "something" << endl;
}
//person.cpp
#include<stdio.h>
#include "person.h"
using namespace std;

int main()
{
	Person p1("Jhon");
	Person p2 = p1;//Person p2(p1);
	printf("p1.name=%p\n", p1.name);
	printf("p2.name=%p\n", p2.name);
	return 0;
}
//main.cpp

指向不同内存:  

什么时候发生拷贝构造:初始化的时候。 

什么时候调用拷贝构造1.函数参数是一个对象的时候,隐藏调用。2.初始化时。3.函数返回一个对象时。

 

 

 

 

 

标签:翁恺,HowMany,name,17,objectCount,C++,Person,print,include
来源: https://blog.csdn.net/qq_40155090/article/details/121953112