C++多线程创建临时对象:类对象隐式转换和显示转换,引用传参和直接传参测试

2022/1/3 12:07:36

本文主要是介绍C++多线程创建临时对象:类对象隐式转换和显示转换,引用传参和直接传参测试,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.隐式转换

#include<iostream>
#include<thread>

using namespace std;


//主线程使用类建立对象,执行顺序测试
class A {
public:
	int m_i;
	//类型转换构造函数,可以把int整型转换成类A对象
	A(int a) :m_i(a) { cout << "A::A(int a)构造函数执行!, 地址是:" << this << "  构造函数线程id是:" << std::this_thread::get_id() << endl; }
	A(const A &a) :m_i(a.m_i) { cout << "A::A(const A)拷贝构造函数执行!地址是:" <<this <<"  拷贝构造函数线程id是:" << std::this_thread::get_id() << endl; }
	~A(){ cout << "A::~A()析构函数执行!" << endl; }
};

void myPrint2(const int i, const A &buf)
{
	cout << "引用的对象地址是"<< &buf << endl;
	cout << "myPrint线程id是" << std::this_thread::get_id() << endl;
	return;
}
int main()
{
	cout << "主线程ID是"<<std::this_thread::get_id() << endl;

	int marv = 1;
	int mySecondPar = 10;
	//显示转换,thread启动时就创建临时对象
	//thread thread2(myPrint2, marv, A(mySecondPar));
	//隐式转换
	thread thread2(myPrint2, marv, mySecondPar);
	
	thread2.join();
	//thread2.detach();	
	return 0;
}
``
![在这里插入图片描述](https://www.www.zyiz.net/i/ll/?i=a58850ab985c47f0bbe54909838af7e2.png)
隐式转换,类对象在子线程里面创建,会带来一定隐患

```cpp
#include<iostream>
#include<thread>

using namespace std;


//主线程使用类建立对象,执行顺序测试
class A {
public:
	int m_i;
	//类型转换构造函数,可以把int整型转换成类A对象
	A(int a) :m_i(a) { cout << "A::A(int a)构造函数执行!, 地址是:" << this << "  构造函数线程id是:" << std::this_thread::get_id() << endl; }
	A(const A &a) :m_i(a.m_i) { cout << "A::A(const A)拷贝构造函数执行!地址是:" <<this <<"  拷贝构造函数线程id是:" << std::this_thread::get_id() << endl; }
	~A(){ cout << "A::~A()析构函数执行!" << endl; }
};

void myPrint2(const int i, const A &buf)
{
	cout << "引用的对象地址是"<< &buf << endl;
	cout << "myPrint线程id是" << std::this_thread::get_id() << endl;
	return;
}
int main()
{
	cout << "主线程ID是"<<std::this_thread::get_id() << endl;

	int marv = 1;
	int mySecondPar = 10;
	//显示转换,thread启动时就创建临时对象
	thread thread2(myPrint2, marv, A(mySecondPar));
	//隐式转换
	//thread thread2(myPrint2, marv, mySecondPar);
	
	thread2.join();
	//thread2.detach();	
	return 0;
}```
![在这里插入图片描述](https://www.www.zyiz.net/i/ll/?i=77f3ece4402a4dcd8f39bffb429f8ee0.png)
引用传参与直接传参的区别
引用传参:类显示转换时,类的构造函数和拷贝构造函数各执行一次。构造函数和拷贝构造函数的所属线程id与主线程一样,说明构造函数在主线程中执行一次,拷贝构造函数在主线程执行一次;见上图
直接传参:类显示转换时,类的构造函数执行一次,拷贝构造函数执行两次次。构造函数在主线程中执行一次,拷贝构造函数在主线程和子线程执行一次;见下图

```cpp
#include<iostream>
#include<thread>

using namespace std;


//主线程使用类建立对象,执行顺序测试
class A {
public:
	int m_i;
	//类型转换构造函数,可以把int整型转换成类A对象
	A(int a) :m_i(a) { cout << "A::A(int a)构造函数执行!, 地址是:" << this << "  构造函数线程id是:" << std::this_thread::get_id() << endl; }
	A(const A &a) :m_i(a.m_i) { cout << "A::A(const A)拷贝构造函数执行!地址是:" <<this <<"  拷贝构造函数线程id是:" << std::this_thread::get_id() << endl; }
	~A(){ cout << "A::~A()析构函数执行!" << endl; }
};

//引用传参
//void myPrint2(const int i, const A &buf)
//直接传参
void myPrint2(const int i, const A buf)
{
	cout << "引用的对象地址是"<< &buf << endl;
	cout << "myPrint线程id是" << std::this_thread::get_id() << endl;
	return;
}
int main()
{
	cout << "主线程ID是"<<std::this_thread::get_id() << endl;

	int marv = 1;
	int mySecondPar = 10;
	//显示转换,thread启动时就创建临时对象
	thread thread2(myPrint2, marv, A(mySecondPar));
	//隐式转换
	//thread thread2(myPrint2, marv, mySecondPar);
	
	thread2.join();
	//thread2.detach();	
	return 0;
}
``![在这里插入图片描述](https://www.www.zyiz.net/i/ll/?i=e0623ac79c324d868c8178f5b9a1d603.png)




这篇关于C++多线程创建临时对象:类对象隐式转换和显示转换,引用传参和直接传参测试的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程