ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++ 17 翁恺> 拷贝构造

2021-12-15 17:05:47  阅读:133  来源: 互联网

标签:翁恺 HowMany name 17 objectCount C++ Person print include


#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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有