我有一些層次結構的類。所以讓我們呼叫第一類硬體,它接收和發送資料,它的每個實體都有一些唯一的引數。隨它去:
class Hardware{
private:
SPI_TypeDef** Instance;
GPIO_Port* Port;
GPIO_Pin Pin;
int Settings;
public
Hardware();
Hardware(GPIO_Port* Port, GPIO_Pin Pin, SPI_HandleTypeDef* SPIx);
void TXRX_Data(Byte* TxBuf, Byte* RxBuf, int Length);
};
然后我有控制外部設備的類,每個設備都有自己的 CS(因為它是 SPI):
class Device{
private:
Hardware Sender;
Byte Parameter_Buffer;
public:
Device();
Device(GPIO_Port* Port, GPIO_Pin Pin, SPI_HandleTypeDef* SPIx);
void SendParameter(Byte Param);
Byte GetParameter();
};
它的建構式只是
Device::Device(GPIO_Port* Port, GPIO_Pin Pin, SPI_HandleTypeDef* SPIx){
this->Sender = Hardware(GPIO_Port* Port, GPIO_Pin Pin, SPI_HandleTypeDef* SPIx);
}
在那之后,我有了主要的控制類
class DeviceArray{
private:
Device Devices[DEVICE_QUANTITY];
Byte Parameters_Buff[DEVICE_QUANTITY];
public:
DeviceArray();
DeviceArray(GPIO_Port** Ports, GPIO_Pin* Pins, SPI_HandleTypeDef* SPIx);
void SendParameters(Byte* Params);
Byte* GetParameters();
}
它的建構式是:
DeviceArray::DeviceArray(GPIO_Port** Ports, GPIO_Pin* Pins, SPI_HandleTypeDef* SPIx){
for(uint8_t i = 0;i < DEVICE_QUANTITY; i ){
this->Devices[i] = Device(Ports[i], Pins[i],SPIx);
}
}
我的程式中只有這個類的一個物件,它將是全域的。
我想知道 DeviceArray 建構式關閉后是否不會洗掉 Device 的實體及其引數。硬體(發件人)實體也是如此。
給定的代碼非常抽象,但它與我正在嘗試制作的程式非常相似。
uj5u.com熱心網友回復:
我想知道 DeviceArray 建構式關閉后是否不會洗掉 Device 的實體及其引數。硬體(發件人)實體也是如此。
是的,這兩種型別的實體都會被銷毀,但這沒關系。
例如線
this->Devices[i] = Device(Ports[i], Pins[i],SPIx);
創建一個臨時Device物件,該物件將在陳述句結束時銷毀,但這并不重要,因為在此之前=將呼叫隱式移動賦值運算子,該運算子會將臨時物件Device的成員移動到物件中。Devicethis->Devices[i]
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/430121.html
下一篇:使用開始和結束迭代器時出錯
