gpt4 book ai didi

c++ - 在复制构造函数定义中调用成员构造函数

转载 作者:行者123 更新时间:2023-12-02 09:59:14 24 4
gpt4 key购买 nike

我试图理解 C++ 中的复制构造函数,并且我想定义自己的复制构造函数,因为我的类中有一个指针成员,所以我需要一个深拷贝。
假设:communicate.h :

#ifndef communicate_h
#define communicate_h

#include "mbed.h"

class Communicate
{
private:
/* data */
BufferedSerial serial;
FILE *serial_stream;
public:
Communicate(const PinName Tx, const PinName Rx, const int baud);
Communicate(const Communicate& source);
~Communicate();
};
#endif
communicate.cpp :
#include "communicate.h"


Communicate::Communicate(const PinName Tx, const PinName Rx, const int baud): serial(Tx, Rx, baud)
{
serial.set_blocking(false);
serial_stream = new FILE;
serial_stream = fdopen(&serial, "w+");
}

Communicate::~Communicate()
{
delete serial_stream;
}

// copy constructor
Communicate::Communicate(const Communicate& source)
{
serial = source.serial;
serial_stream = new FILE;
*serial_stream = *source.serial_stream;
}
我明白了:
Compile [ 99.5%]: communicate.cpp
[Error] communicate.cpp@17,51: no matching function for call to 'mbed::BufferedSerial::BufferedSerial()'
[Error] communicate.cpp@19,21: use of deleted function 'mbed::BufferedSerial& mbed::BufferedSerial::operator=(const mbed::BufferedSerial&)'
[Error] BufferedSerial.h@52,7: use of deleted function 'mbed::SerialBase& mbed::SerialBase::operator=(const mbed::SerialBase&)'
[Error] SerialBase.h@46,7: use of deleted function 'mbed::NonCopyable<T>& mbed::NonCopyable<T>::operator=(const mbed::NonCopyable<T>&) [with T = mbed::SerialBase]'
[Error] SerialBase.h@46,7: non-static const member 'const PinName mbed::SerialBase::_tx_pin', can't use default assignment operator
[Error] SerialBase.h@46,7: non-static const member 'const PinName mbed::SerialBase::_rx_pin', can't use default assignment operator
[Error] BufferedSerial.h@52,7: use of deleted function 'mbed::FileHandle& mbed::FileHandle::operator=(const mbed::FileHandle&)'
[Error] FileHandle.h@46,7: use of deleted function 'mbed::NonCopyable<T>& mbed::NonCopyable<T>::operator=(const mbed::NonCopyable<T>&) [with T = mbed::FileHandle]'
[Error] BufferedSerial.h@52,7: use of deleted function 'mbed::NonCopyable<T>& mbed::NonCopyable<T>::operator=(const mbed::NonCopyable<T>&) [with T = mbed::BufferedSerial]'
[Error] BufferedSerial.h@52,7: use of deleted function 'rtos::Mutex& rtos::Mutex::operator=(const rtos::Mutex&)'
[Error] Mutex.h@70,7: use of deleted function 'mbed::NonCopyable<T>& mbed::NonCopyable<T>::operator=(const mbed::NonCopyable<T>&) [with T = rtos::Mutex]'
错误提示在 Communicate的复制构造函数中没有调用BufferedSerial类的构造函数另一方面,我不能简单地把 : serial(Tx, Rx, baud)在复制构造函数定义之前。
我不确定如何在复制构造函数定义中调用成员构造函数。

最佳答案

错误信息告诉你 BufferedSerial不可复制分配:

[Error] communicate.cpp@17,51: no matching function for call to 'mbed::BufferedSerial::BufferedSerial()'
[Error] communicate.cpp@19,21: use of deleted function 'mbed::BufferedSerial& mbed::BufferedSerial::operator=(const mbed::BufferedSerial&)'
在错误消息的末尾,您会看到:
[Error] Mutex.h@70,7: use of deleted function 'mbed::NonCopyable<T>& mbed::NonCopyable<T>::operator=(const mbed::NonCopyable<T>&) [with T = rtos::Mutex]'
一个类只有在其所有成员都是默认复制构造和复制可分配的情况下。由于互斥锁的工作方式,它是不可复制的,并且因为互斥锁被 BufferedSerial 的成员之一使用。 BufferedSerial默认情况下,它本身不会是复制构造和复制可分配,而您的 Communicate不应该是因为这个。
如果你想为你的类写一个拷贝构造函数和拷贝赋值运算符,那么你需要弄清楚如何才能有两个不同的 FILE在同一个文件/管道上工作,这通常没有多大意义(或如何在实例之间共享它们)。因为如果你这样做,尤其是使用 w+如果流被缓冲,您肯定会遇到数据损坏问题(即使不使用互斥锁,它也肯定不可复制的另一个原因)。
这表明您在设计中可能存在误解。

关于c++ - 在复制构造函数定义中调用成员构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63544365/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com