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.icode9.com/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.icode9.com/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.icode9.com/i/ll/?i=e0623ac79c324d868c8178f5b9a1d603.png)