C++学习-拷贝构造函数

引言:

#include<iostream>
using namespace std;
class Student
{
public:
Student()
{
cout<<"Student"<<endl;

private:
string m_strName;
} ;
int main()
{
Student stu1;
Student stu2=stu1;
Student stu3(stu1);
}


按照这一种代码,进行stu2和stu3进行初始化的时候,我们发现我们的程序只输出了一个“Student”,按照我们之前学习过的知识,初始化一个类型成员的时候一定要调用构造函数,问题出现在哪呢?

问题出现在:我们这样子调用的是拷贝构造函数而没有调用Student类里面的默认构造函数


拷贝构造函数的名称和类名是一样的,但是参数和默认构造函数有所区别

在这里,就应该这样子写

Student(const Student&stu){ };

这样子我们就写好了我们的拷贝构造函数

为了区别,我们在里面写上

cout<<"copy Student"<<endl;


在这里我试了一下const Student&stu和Student const&stu都可以过编译


总结:

1、当我们没有自己定义一个默认构造函数和拷贝构造函数的时候我们的编译器就会自己帮我们弄一个默认构造函数、拷贝构造函数

但是当我们定义了一个这样子的东西之后我们的编译器就不会帮我们弄了,因为我们已经有了我们自己的默认构造函数和拷贝构造函数。

2、很关键的一点是,构造函数分成两个大类,带参的构造函数和不带参的构造函数,其中不带参的构造函数和带的参数都有默认值的构造函数

统统叫做默认构造函数

3、初始化列表可以跟在构造函数后面,所有,构造函数后面

#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
	Student() :m_strName("Adison"), m_intnumber(20318)
	{
		cout << "Student" << endl;
	}
	Student(const Student&stu) :m_strName("Alice"), m_intnumber(20301)
	{
		cout << "copy Student" << endl;
	}
	string printfName()
	{
		return m_strName;
	}
	int printfNum()
	{
		return m_intnumber;
	}
private:
	string m_strName;
	int m_intnumber;
};
int main()
{
	Student stu1;
	cout << stu1.printfName() << endl;
	cout << stu1.printfNum() << endl;
	Student stu2 = stu1;
	cout << stu2.printfName() << endl;
	cout << stu2.printfNum() << endl;
	Student stu3(stu1);
	cout << stu3.printfName() << endl;
	cout << stu3.printfNum() << endl;
	system("pause");
}


#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
	Student(string a) :m_strName("Adison"), m_intnumber(20318)
	{
		setName2(a);
		cout << "Student" << endl;
	}
	Student(const Student&stu) :m_strName("Alice"), m_intnumber(20301)
	{
		setName();
		cout << "copy Student" << endl;
	}
	string printfName()
	{
		return m_strName;
	}
	int printfNum()
	{
		return m_intnumber;
	}
	void setName()
	{
		m_strName="auto";
	}
	void setName2(string a)
	{
		m_strName=a;
	} 
private:
	string m_strName;
	int m_intnumber;
};
int main()
{
	Student stu1("didi");
	cout << stu1.printfName() << endl;
	cout << stu1.printfNum() << endl;
	Student stu2 = stu1;
	cout << stu2.printfName() << endl;
	cout << stu2.printfNum() << endl;
	Student stu3(stu1);
	cout << stu3.printfName() << endl;
	cout << stu3.printfNum() << endl;
	system("pause");
}

这里有两份我写的代码可以给大家玩一下hh~

但这里我只是自己有点不懂是不是所有构造函数都可以跟初始化列表,我试了一下发现是可以的~

另外要明确的一点是,我们的初始化列表执行顺序在构造函数的内容之前,所以说会有被覆盖的危险。


另外,我们如果调用了一个含Student类的参数的函数的时候,也会触发拷贝构造函数

例如下面这个例子

#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
	Student(string a) :m_strName("Adison"), m_intnumber(20318)
	{
		setName2(a);
		cout << "Student" << endl;
	}
	Student(const Student&stu) :m_strName("Alice"), m_intnumber(20301)
	{
		setName();
		cout << "copy Student" << endl;
	}
	string printfName()
	{
		return m_strName;
	}
	int printfNum()
	{
		return m_intnumber;
	}
	void setName()
	{
		m_strName="auto";
	}
	void setName2(string a)
	{
		m_strName=a;
	} 
private:
	string m_strName;
	int m_intnumber;
};
void copy(Student a)
{
}
int main()
{
	Student stu1("didi");

	Student stu2 = stu1;

	Student stu3(stu1);

	copy(stu2);
	system("pause");
}

PS:拷贝构造函数是不可重载的,是确定的


另外,从堆中构建一个类的对象,调用的是普通的构造函数,并不是调用拷贝构造函数

#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
	Student(string a) :m_strName("Adison"), m_intnumber(20318)
	{
		setName2(a);
		cout << "Student" << endl;
	}
	Student(const Student&stu) :m_strName("Alice"), m_intnumber(20301)
	{
		setName();
		cout << "copy Student" << endl;
	}
	string printfName()
	{
		return m_strName;
	}
	int printfNum()
	{
		return m_intnumber;
	}
	void setName()
	{
		m_strName="auto";
	}
	void setName2(string a)
	{
		m_strName=a;
	} 
	~Student()
	{
		cout<<"~Student"<<endl;
	}
private:
	string m_strName;
	int m_intnumber;
};
void copy(Student a)
{
}
int main()
{
	Student stu1("didi");
	Student *stu2 = new Student("didi");
	cout<<stu2->printfName()<<endl;
	delete stu2;
	system("pause");
}

另外,我还试了一下,发现“=”操作在初始化的时候是可以直接用的,但是在初始化之后用就会报错,我想这需要运算符重载了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
编译器在优化时可以避免调用拷贝构造函数,而改为调用移动构造函数。编译器实现这种优化的原理是,当一个函数返回一个class对象时,如果该对象是一个临时对象或者即将被销毁的对象,编译器可以将该对象的资源直接转移到返回值,而不是进行拷贝构造。如果程序员没有自己实现移动构造函数,编译器会默认生成一个移动构造函数拷贝构造函数的作用是为一个新对象复制一份和其他对象一模一样的数据。当类中包含指针类型的成员变量时,在拷贝构造函数中需要以深拷贝的方式复制该指针成员。例如,在自己简单实现的string类中,拷贝构造函数使用了深拷贝方式复制指针m_ptr的内容。 移动构造函数C++11引入的新概念,它的作用是将一个对象的资源转移到另一个对象,同时将原对象置为有效但不可用的状态。移动构造函数通常通过右值引用(&&)来接收参数。在自己实现的string类中,移动构造函数使用了memcpy函数将原对象的资源转移到新对象中,并将原对象的指针m_ptr置为nullptr。 总结来说,拷贝构造函数用于创建一个新对象并复制数据,而移动构造函数用于将一个对象的资源转移到另一个对象。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [C++学习笔记3:拷贝构造和移动构造](https://blog.csdn.net/pdx_ll/article/details/123882008)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [从零开始的移动构造函数拷贝构造函数详解(C++)](https://blog.csdn.net/balcklist/article/details/126341235)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值