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

马苏德·拉希米(Masoud Rahimi)

我试图理解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]'

该错误抱怨无法在类的副本构造函数中调用BufferedSerial类的构造函数,Communicate另一方面,我不能简单地将: serial(Tx, Rx, baud)in放在副本构造函数定义的前面。

我不确定如何在复制构造函数定义中调用成员构造函数。

该错误消息告诉您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]'

一个类仅是默认的副本构造,并且如果其所有成员均是可分配的,则该副本可分配。由于如何互斥的作品,它是不可拷贝,因为互斥体使用的成员之一BufferedSerialBufferedSerial本身不会拷贝构造和默认复制分配,和你的Communicate不应该是由于这一点。

如果要为您的类编写一个复制构造函数和一个复制赋值运算符,则需要弄清楚如何FILE在同一个文件/管道上使用两个不同的对象,这通常没有多大意义(或如何共享它们)实例之间)。因为如果这样做,尤其是对于a这样做,w+则很可能在缓冲流的情况下遇到数据损坏的问题(这是即使不使用互斥锁也无法复制的另一个原因)。

这表明您可能对设计有误解。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章