c++:在哪里删除对象并将对象转换回父类的成员

标签 c++ oop serial-port

我有一个简单的 Message 类和一个简单的 SerialPort 类。我还有一个特定的消息子类和一个特定的串口子类(CustomMessage & CustomSerialPort):

class Message
{
public: 
    uint8 getLength() const ( return m_length; }
    const uint8* getData() const { return m_contents; }
...
}

class SerialPort
{
public:
    bool OpenSerial(int32& errcode);  
    bool ReadFromSerial(int32& errcode, Message& msg);  
    bool WriteToSerial(int32& errcode, Message& msg, 
        uint32* const nBytesWritten);
...
}

这是自定义类。请注意,我重载了 WriteToSerial() 以获取 CustomMessage 而不仅仅是 Message。

class CustomSerialPort : public SerialPort
{
public:
    bool WriteToSerial(int32& errcode, CustomMessage& msg, 
        uint32* const nBytesWritten);
...
}

class CustomMessage : public Message 
{
    // lots of stuff for messages to specific device
}

同样重要的是,CustomSerial::WriteToSerial 和 CustomMessage::toMessage() 的实现

bool CustomSerialPort::WriteToSerial(int32& errcode, CustomMessage& msg, 
    uint32* const nBytesWritten)
{
    SerialPort::WriteToSerial(errcode, msg.toMessage(), nBytesWritten);
}

Message& CustomMessage::toMessage() 
{
    Message* msg = new Message(m_contents, m_length);
    return *msg;
}

您可以看到我调用了 SerialPort 类的 WriteToSerial 并向它发送了一个已转换为 Message 的 CustomMessage。

我的问题是:我应该在哪里删除我创建的传递给 SerialPort::WriteToSerial 的消息?

或者,我应该做更多这样的事情:

bool CustomSerialPort::WriteToSerial(int32& errcode, CustomMessage& msg, 
    uint32* const nBytesWritten)
{
    // don't use new
    Message m(msg);

    SerialPort::WriteToSerial(errcode, m, nBytesWritten);
    // deleted when goes out of scope
}

然后,对于选项 2,如果我的理解是正确的,我只需要制作一个 Message 构造函数,该构造函数采用 CustomMessage 的参数......等等......这看起来很奇怪......在父级中采用子类对象参数类构造函数。我需要重新考虑这个吗?

最佳答案

您不需要在 toMessage() 中 new Message 也不需要删除它。

改变

Message& CustomMessage::toMessage() 
{
    Message* msg = new Message(m_contents, m_length);
    return *msg;
}

bool CustomSerialPort::WriteToSerial(int32& errcode, CustomMessage& msg, 
    uint32* const nBytesWritten);

Message CustomMessage::toMessage() 
{
    return Message(m_contents, m_length);
}

bool CustomSerialPort::WriteToSerial(int32& errcode, const CustomMessage& msg, 
                                                     ^^^ const
    uint32* const nBytesWritten)

toMessage()在WriteToSerial中被调用时,它将被绑定(bind)直到WriteToSerial()函数完成。

您还需要为所有将Message 作为输入的函数添加const 限定符

class SerialPort
{
public:
    bool OpenSerial(int32& errcode);  
    bool ReadFromSerial(int32& errcode, const Message& msg);  
    bool WriteToSerial(int32& errcode, const Message& msg, 
        uint32* const nBytesWritten);
...
}

关于c++:在哪里删除对象并将对象转换回父类的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14419738/

相关文章:

c++ - Qt错误: symbol(s) not found for architecture x86_64 on Mac OS X Yosemite

c++ - 是否保证容器会在 std::move 期间破坏现有对象?

c++ - 服务器客户端通过原始数据错误发送接收结构 C++

c - Linux二进制串行读取问题

linux - LINUX 中的串行通信 (RS232)

c# - C#中如何从串口读取字节数组

java - 我应该使用哪种键值数据结构?按值快速检索,按键快速检索

go - go编程语言中的任何类型和实现泛型列表

c++ - 更多关于中介者模式和 OO 设计

c++ - 前向声明后类型类的使用无效