Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9175627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:56:49+00:00 2026-06-17T16:56:49+00:00

I have a simple Message class and a simple SerialPort Class. I also have

  • 0

I have a simple Message class and a simple SerialPort Class. I also have a specific message subclass and a specific serial port subclass (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);
...
}

Here are the custom classes. Note that I overloaded the WriteToSerial() to take CustomMessage instead of just 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
}

Also important, the implementation of CustomSerial::WriteToSerial and 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;
}

You can see that I call the WriteToSerial of the SerialPort class and send it a CustomMessage that has been converted to a Message.

My question is this: where should I delete the message that I created to pass to SerialPort::WriteToSerial?

Or, should I do something more like this:

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
}

Then, with option 2, if my understanding is correct, I just need to make a Message constructor that takes a parameter of CustomMessage… wait… that seems weird.. taking a child class object parameter in a parent class constructor. Do I need to rethink this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:56:50+00:00Added an answer on June 17, 2026 at 4:56 pm

    You don’t need to new Message inside toMessage() and you don’t need to delete it.

    Change

    Message& CustomMessage::toMessage() 
    {
        Message* msg = new Message(m_contents, m_length);
        return *msg;
    }
    
    bool CustomSerialPort::WriteToSerial(int32& errcode, CustomMessage& msg, 
        uint32* const nBytesWritten);
    

    to

    Message CustomMessage::toMessage() 
    {
        return Message(m_contents, m_length);
    }
    
    bool CustomSerialPort::WriteToSerial(int32& errcode, const CustomMessage& msg, 
                                                         ^^^ const
        uint32* const nBytesWritten)
    

    When toMessage() is called in WriteToSerial, it will be bind till WriteToSerial() function finishes.

    Also you need to add const qualifier to all functions take Message as input

    class SerialPort
    {
    public:
        bool OpenSerial(int32& errcode);  
        bool ReadFromSerial(int32& errcode, const Message& msg);  
        bool WriteToSerial(int32& errcode, const Message& msg, 
            uint32* const nBytesWritten);
    ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this simple interface/class: public abstract class Message {} public class Message1 extends
I have a simple class that looks like this: [DataContract] public class Actor {
I have a simple WPF windows application trying to read a serial port with
I have a simple ViewModel public class TestViewModel : ViewModelBase, IDisposable { public TestViewModel()
I have a simple class like this, import javax.validation.constraints.NotNull; import org.hibernate.validator.constraints.Length; public class Form
Currently I have a simple class for users, like the follow: public class Users
I have a simple c2dm message receiver class which is called whenever the device
I have a simple loginform based on the following modelitem public class LogOnModelItem {
I need some help with this one.... I have this simple model: public class
I have a simple Message table, with 2 indexes: mysql> show keys from Message;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.