什么';在这个构造函数和析构函数中发生了什么? P>多年来,我不得不回到使用C++。我有点生疏了,有人能解释一下在主功能中构建和解构对象时发生了什么吗

什么';在这个构造函数和析构函数中发生了什么? P>多年来,我不得不回到使用C++。我有点生疏了,有人能解释一下在主功能中构建和解构对象时发生了什么吗,c++,class,vector,constructor,C++,Class,Vector,Constructor,该代码正在作为intendet工作。奇怪的是,如果我调试代码,调试器会在构建类时停止执行两次,在销毁类时停止执行三次。但是它会在代码的一部分停止,在那里没有调试信息是可获得的(是的,gcc正在生成我的调试信息)。我想对此进行调查,但我不确定代码到底做了什么 代码如下: class LSPI_BusSymulation_SlaveDevice; class LSPI_BusSymulation{ public: LSPI_BusSymulation(uint16_t dataSizePe

该代码正在作为intendet工作。奇怪的是,如果我调试代码,调试器会在构建类时停止执行两次,在销毁类时停止执行三次。但是它会在代码的一部分停止,在那里没有调试信息是可获得的(是的,gcc正在生成我的调试信息)。我想对此进行调查,但我不确定代码到底做了什么

代码如下:

class LSPI_BusSymulation_SlaveDevice;

class LSPI_BusSymulation{
public:
    LSPI_BusSymulation(uint16_t dataSizePerDevice, uint16_t maxNumberOfDevices, uint16_t actualNumberOfDevices);
private:
    uint16_t actualNumberOfDevices;
    std::vector<LSPI_BusSymulation_SlaveDevice> devices;
    std::vector<uint8_t> recvData;
};

class LSPI_BusSymulation_SlaveDevice{
public:
    LSPI_BusSymulation_SlaveDevice(uint16_t processDataSizePerDevice, uint16_t maxNumberOfDevices);
private:
    uint16_t dataSizePerDevice;
    std::vector<uint8_t> data;
};

LSPI_BusSymulation::LSPI_BusSymulation(uint16_t dataSizePerDevice, uint16_t maxNumberOfDevices, uint16_t actualNumberOfDevices)
    : actualNumberOfDevices(actualNumberOfDevices){    
    recvData.resize(dataSizePerDevice * (maxNumberOfDevices + 1));
    devices.assign(actualNumberOfDevices, LSPI_BusSymulation_SlaveDevice(dataSizePerDevice, maxNumberOfDevices));
}

LSPI_BusSymulation_SlaveDevice::LSPI_BusSymulation_SlaveDevice(uint16_t processDataSizePerDevice, uint16_t maxNumberOfDevices)
    : dataSizePerDevice(processDataSizePerDevice){
    data.resize(processDataSizePerDevice * (maxNumberOfDevices + 1 + 1));
}

int main(){    
    LSPI_BusSymulation busSymulation(10, 10, 10);
    return 0;
}
class LSPI\u总线符号\u SlaveDevice;
类LSPI_总线符号{
公众:
LSPI总线符号(uint16数据集设备、uint16最大设备数、uint16实际设备数);
私人:
uint16_t设备的实际数量;
std::矢量设备;
std::向量recvData;
};
LSPI类\u总线符号\u辅助设备{
公众:
LSPI总线符号辅助设备(uint16处理数据集设备,uint16最大设备数);
私人:
uint16_t数据集设备;
std::矢量数据;
};
LSPI_总线符号化::LSPI_总线符号化(uint16数据集设备、uint16最大设备数、uint16实际设备数)
:实际设备数(实际设备数){
resize(dataSizePerDevice*(maxNumberOfDevices+1));
分配(实际设备数量、LSPI_总线符号化_SlaveDevice(数据集设备、maxNumberOfDevices));
}
LSPI_总线符号化_辅助设备::LSPI_总线符号化_辅助设备(uint16_t ProcessDataSizepDevice,uint16_t maxNumberOfDevices)
:dataSizePerDevice(processDataSizePerDevice){
调整大小(processDataSizePerDevice*(maxNumberOfDevices+1+1));
}
int main(){
LSPI_总线符号化总线符号化(10,10,10);
返回0;
}
对我来说重要的问题是:(但如果你有时间,一步一步的解释也会有帮助)

LSPI_SYMULATION_SLAVE_设备的构造函数和析构函数多久调用一次

是否所有对象都存储在BusSymation.devices中,并使用各自的内存将对象分开

向量BusSymation.devices[n]。数据是否也被复制,或者这些设备是否共享一个数据向量

的构造函数和析构函数的频率是多少 LSPI\u符号化\u从\u设备被调用

构造器

LSPI_BusSymulation_SlaveDevice::LSPI_BusSymulation_SlaveDevice(uint16_t processDataSizePerDevice, uint16_t maxNumberOfDevices)
只叫一次。复制构造函数被调用10次。解构师被叫了11次

是否所有对象都存储在BusSymation.devices中,并使用各自的内存将对象分开

向量BusSymation.devices[n]。数据是否也被复制,或者这些设备是否共享一个数据向量


它被复制了。

也许你需要读什么和做什么?@怪怪的,我确实做了。但我不确定我是否理解正确。对于赋值:我想,一个常量对象被赋值了。然后该对象被复制多次。所以构造函数只调用一次。删除此向量时,obecjts析构函数称为vector.size()+1次。但我不是很确定。一切正常,但调试器的行为异常,使我认为我理解了一些错误。