昨天说到:vector变量push_back一个对象或变量的时候,本质上是执行拷贝构造,但我想使用移动构造,而不是拷贝构造,本文就修改调试过程,详细分析如何实现移动构造。
昨天的代码如下:(如果有人想测试,可以直接copy)
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <utility>
typedef struct {
std::string strPortName;
unsigned int dwBaudRate;
unsigned char bByteSize;
unsigned char bStopBits;
unsigned char bParity;
} TCommPara;
class CComm {
public:
void config() {
// 配置串口
}
};
class CMeterProto {
public:
virtual bool Init(unsigned char bProp, unsigned char bPn, CComm *pComm) { return false; };
virtual bool Run() { return false; };
unsigned char m_bPn; //当前操作的测量点
unsigned char m_bProp; //设备类型
TCommPara m_tCommPara; //通讯参数
protected:
virtual int GetData() { return -1; };
virtual int SetData() { return -1; };
};
class CModbusProto : public CMeterProto {
public:
CModbusProto(const unsigned char bBAPn, const TCommPara tCommPara)
: m_bBAPn(bBAPn), m_tCommPara(tCommPara) {
// 构造函数实现
}
protected:
virtual int GetData() override;
private:
unsigned char m_bBAPn;
TCommPara m_tCommPara;
};
int CModbusProto::GetData()
{
return 0;
}
class T485CommCtrlPara
{
public:
T485CommCtrlPara() = default;
T485CommCtrlPara(const T485CommCtrlPara& other);
virtual ~T485CommCtrlPara() = default;
std::unique_ptr<CMeterProto> pComm485Pro;
CComm cMterCom;
};
T485CommCtrlPara::T485CommCtrlPara(const T485CommCtrlPara& other)
{
pComm485Pro = nullptr;
if (other.pComm485Pro)
{
// pComm485Pro = std::make_unique<CMeterProto>(*other.pComm485Pro);
pComm485Pro = std::unique_ptr<CMeterProto>(new CMeterProto(*other.pComm485Pro));
}
cMterCom = other.cMterCom;
std::cout << "copy-ctor" << std::endl;
}
int LoadBA485CommPara(TCommPara& tCommPara, T485CommCtrlPara& t485CommPara, std::vector<T485CommCtrlPara>& vec485DevCommPara) {
tCommPara.strPortName = "com1";
tCommPara.dwBaudRate = 9600;
tCommPara.bByteSize = 8;
tCommPara.bStopBits = 1;
t485CommPara.cMterCom.config();
std::unique_ptr<CMeterProto> pComm485Pro(new CModbusProto(1, tCommPara));
t485CommPara.pComm485Pro = std::move(pComm485Pro);
vec485DevCommPara.push_back(t485CommPara);
return 1;
}
int main() {
TCommPara tCommPara;
T485CommCtrlPara t485CommPara;
std::vector<T485CommCtrlPara> vec485DevCommPara;
int nBaNum = LoadBA485CommPara(tCommPara, t485CommPara, vec485DevCommPara);
vec485DevCommPara[0].pComm485Pro->Run();
std::cout << vec485DevCommPara.size() << std::endl;
std::getchar();
return 0;
}
为了使用移动构造,先修改class T485CommCtrlPara构造函数如下:
注意,开始的时候,编写移动赋值,在operator=之前,没有添加T485CommCtrlPara::
结果一直就提示:“ error C2801: “operator =”必须是非静态成员”。
(之前代码是chatgpt写的,有语法错误,提示后,chatgpt更正了错误)
如上代码修改完成后,可以编译和运行,但push_back还是提示使用了拷贝构造函数(查看调试信息),如果要用移动,需要修改为:
如上,显然就调用了移动,查看运行信息,确实调用了move-ctor。
但这只是一个示例程序,实际程序中,不可能在main中定义很多变量执行的,因此,考虑在main之外定义一个函数处理vec485DevCommPara,增加一个接口如下:
代码编译ok,也能运行,但查看内存,发现异常了。
内存异常!!
原因分析:
Load485CommPara函数定义了局部变量,
T485CommCtrlPara t485CommPara;
TCommPara tCommPara;
该函数执行完后,这两个变量会销毁,t485CommPara类如下:
class T485CommCtrlPara
{
public:
T485CommCtrlPara() = default;
T485CommCtrlPara(const T485CommCtrlPara& other);
virtual ~T485CommCtrlPara() = default;
std::unique_ptr<CMeterProto> pComm485Pro;
CComm cMterCom;
};
这样,在vec485DevCommPara.emplace_back(std::move(t485CommPara))的时候,pComm485Pro移动了,而CComm cMterCom;只是成员变量,如此移动并不成功。
T485CommCtrlPara::T485CommCtrlPara(const T485CommCtrlPara& other)
{
pComm485Pro = nullptr;
if (other.pComm485Pro)
{
pComm485Pro = std::unique_ptr<CMeterProto>(new CMeterProto(*other.pComm485Pro));
}
cMterCom = other.cMterCom;
// cMterCom = std::move(other.cMterCom);
std::cout << "copy-ctor" << std::endl;
}
如上两种都不成功,最好办法就是CComm cMterCom也定义为智能指针,如下:
class T485CommCtrlPara
{
public:
T485CommCtrlPara() = default;
T485CommCtrlPara(const T485CommCtrlPara& other);
T485CommCtrlPara(T485CommCtrlPara&& other) noexcept;
T485CommCtrlPara& operator=(T485CommCtrlPara&& other) noexcept;
virtual ~T485CommCtrlPara() = default;
std::unique_ptr<CMeterProto> pComm485Pro;
std::unique_ptr<CComm> pComm485;
};
T485CommCtrlPara::T485CommCtrlPara(const T485CommCtrlPara& other)
{
pComm485Pro = nullptr;
if (other.pComm485Pro)
{
pComm485Pro = std::unique_ptr<CMeterProto>(new CMeterProto(*other.pComm485Pro));
pComm485 = std::unique_ptr<CComm>(new CComm(*other.pComm485));
}
std::cout << "copy-ctor" << std::endl;
}
T485CommCtrlPara::T485CommCtrlPara(T485CommCtrlPara&& other) noexcept
{
pComm485Pro = std::move(other.pComm485Pro);
pComm485 = std::move(other.pComm485);
std::cout << "move-ctor" << std::endl;
}
完成后,运行基本正常了:
如上,已经可以正常运行了,但还是一点问题,就是pComm485Pro的基类成员m_tCOmmPara还是为空的。这是因为在new CModbusProto(1, tCommPara)的时候,调用了CModbusProto的构造,但基类CMeterProto并没有构造函数(或者只有默认构造,但并没有赋值),所以,虽然基类也定义了m_tCommPara,但实际并没有赋值。如何更完美呢?
增加基类构造,同时修改下继承类的构造,如下:
先修改基类,增加构造函数:
class CMeterProto {
public:
CMeterProto(const unsigned char& bPn, const TCommPara& tCommPara) :m_bPn(bPn), m_tCommPara(tCommPara) {}
virtual bool Init(unsigned char bProp, unsigned char bPn, CComm *pComm);
virtual int Run();
unsigned char m_bProp; //设备类型
protected:
virtual int GetData();
virtual int SetData() { return -1; };
unsigned char m_bPn; //当前操作的测量点
TCommPara m_tCommPara; //通讯参数
CComm *m_pComm;
};
class CModbusProto : public CMeterProto
{
public:
CModbusProto(const unsigned char bBAPn, const TCommPara tCommPara):
CMeterProto { bBAPn, tCommPara } {}
//unsigned char m_bBAPn; //当前操作的测量点
//TCommPara m_tCommPara; //通讯参数
protected:
virtual int GetData() override;
};
如上修改后,编译运行,数据正确,结果如下:
如上,运行正常了,我将完整代码贴到下面
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <utility>
typedef struct {
std::string strPortName;
unsigned int dwBaudRate;
unsigned char bByteSize;
unsigned char bStopBits;
unsigned char bParity;
} TCommPara;
class CComm {
public:
void config(const TCommPara&);
private:
TCommPara m_tCommPara;
};
void CComm::config(const TCommPara& tCommPara)
{
m_tCommPara = tCommPara;
}
class CMeterProto {
public:
CMeterProto(const unsigned char& bPn, const TCommPara& tCommPara) :m_bPn(bPn), m_tCommPara(tCommPara) {}
virtual bool Init(unsigned char bProp, unsigned char bPn, CComm *pComm);
virtual int Run();
protected:
virtual int GetData();
virtual int SetData() { return -1; };
unsigned char m_bPn; //当前操作的测量点
TCommPara m_tCommPara; //通讯参数
unsigned char m_bProp; //设备类型
CComm *m_pComm;
};
bool CMeterProto::Init(unsigned char bProp, unsigned char bPn, CComm *pComm)
{
m_bProp = bProp;
m_bPn = bPn;
m_pComm = pComm;
return true;
}
int CMeterProto::GetData()
{
std::cout << "CMeterProto-GetData" << std::endl;
return -1;
}
int CMeterProto::Run()
{
return GetData();
}
class CModbusProto : public CMeterProto
{
public:
CModbusProto(const unsigned char bBAPn, const TCommPara tCommPara):
CMeterProto { bBAPn, tCommPara } {}
//unsigned char m_bBAPn; //当前操作的测量点
//TCommPara m_tCommPara; //通讯参数
protected:
virtual int GetData() override;
};
int CModbusProto::GetData()
{
std::cout << "CModbusProto-GetData" << std::endl;
return 0;
}
class T485CommCtrlPara
{
public:
T485CommCtrlPara() = default;
T485CommCtrlPara(const T485CommCtrlPara& other);
T485CommCtrlPara(T485CommCtrlPara&& other) noexcept;
T485CommCtrlPara& operator=(T485CommCtrlPara&& other) noexcept;
virtual ~T485CommCtrlPara() = default;
std::unique_ptr<CMeterProto> pComm485Pro;
std::unique_ptr<CComm> pComm485;
};
T485CommCtrlPara::T485CommCtrlPara(const T485CommCtrlPara& other)
{
pComm485Pro = nullptr;
if (other.pComm485Pro && other.pComm485)
{
pComm485Pro = std::unique_ptr<CMeterProto>(new CMeterProto(*other.pComm485Pro));
pComm485 = std::unique_ptr<CComm>(new CComm(*other.pComm485));
}
std::cout << "copy-ctor" << std::endl;
}
T485CommCtrlPara::T485CommCtrlPara(T485CommCtrlPara&& other) noexcept
{
if (this != &other && other.pComm485Pro && other.pComm485)
{
pComm485Pro = std::move(other.pComm485Pro);
pComm485 = std::move(other.pComm485);
}
std::cout << "move-ctor" << std::endl;
}
T485CommCtrlPara& T485CommCtrlPara::operator=(T485CommCtrlPara&& other) noexcept
{
if (this != &other)
{
pComm485Pro = std::move(other.pComm485Pro);
pComm485 = std::move(other.pComm485);
}
std::cout << "move-operator-ctor" << std::endl;
return *this;
}
int LoadBA485CommPara(TCommPara& tCommPara, T485CommCtrlPara& t485CommPara, std::vector<T485CommCtrlPara>& vec485DevCommPara)
{
tCommPara.strPortName = "com1";
tCommPara.dwBaudRate = 9600;
tCommPara.bByteSize = 8;
tCommPara.bStopBits = 1;
std::unique_ptr<CMeterProto> pComm485Pro(new CModbusProto(1, tCommPara));
std::unique_ptr<CComm> pComm485(new CComm());
pComm485->config(tCommPara);
t485CommPara.pComm485Pro = std::move(pComm485Pro);
t485CommPara.pComm485 = std::move(pComm485);
t485CommPara.pComm485Pro->Init(1, 1, t485CommPara.pComm485.get());
vec485DevCommPara.emplace_back(std::move(t485CommPara));
return 1;
}
int Load485CommPara(std::vector<T485CommCtrlPara>& vec485DevCommPara)
{
T485CommCtrlPara t485CommPara;
TCommPara tCommPara;
return LoadBA485CommPara(tCommPara, t485CommPara, vec485DevCommPara);
}
int main()
{
std::vector<T485CommCtrlPara> vec485DevCommPara;
Load485CommPara(vec485DevCommPara);
vec485DevCommPara[0].pComm485Pro->Run();
std::cout << vec485DevCommPara.size() << std::endl;
std::getchar();
return 0;
}
总结:
1:智能指针方便内存管理,但复杂度提升了不少,光有理论不够,还需要经常练习总结啊
2:chatgpt或newbing会帮忙不少,但不能太依赖了,你需要问正确的问题,但有一个专家在身边,确实能提供很大帮助啊
3:容器,智能指针是必须要现代C++的基操了,赶紧都用上吧