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 9208219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:34:12+00:00 2026-06-18T00:34:12+00:00

I am changing my current implementation of a message class from: class Message {

  • 0

I am changing my current implementation of a message class from:

class Message
{
public:
    Message(uint8* buffer, uint8 length) : m_contents(buffer), m_length(length) { }

    uint8 getLength() const {return m_length; }
    const uint8* getData() const { return m_contents; }
protected:
    uint8* m_contents;
    uint8 m_length;
};

To take only length in the constructor, and dynamically allocate in the constructor, and delete in the destructor.

I think it should look like:

Message(uint8 length) : m_length(length)
{
    m_contents = new uint8[length];
}

~Message() 
{
    delete [] m_contents;
    m_contents = NULL;
}

Additional info: The message object will be passed to a serial port object which will send it off. It will also be subclassed into multiple CustomMessage classes.

My question is: in this simple example, are there any reasons to use some type of smart pointer? An example would be helpful. Thanks!

  • 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-18T00:34:13+00:00Added an answer on June 18, 2026 at 12:34 am

    are there any reasons to use some type of smart pointer?

    Actually it depends. Consider this code:

     Message m1 (1024);
     Message m2 (m1);
    

    Do you want m2 to be a deep-copy of m1? Do you want the underlying buffer of m1 and m2 to share memory? Or you don’t want the copy to begin with (deep or otherwise)? That is, the second statement should result in compilation error?

    • If you want them to share memory, then yes, you should use std::shared_ptr.
    • If you want them to be completely different copy in memory (deep-copy), then no, you should not use shared_ptr; you can use std::unique_ptr though, but you have to implement copy-semantic also. But then if you go with this case, then it is better to use std::vector<uint8> instead of implementing your own class:

      typedef std::vector<uint8> Message;
      
      Message m1(1024);
      Message m2(m1); //m2 is a deep-copy of m1
      
    • If you want to prevent copy and thus the second statement should result in compilation error, then you’ve to disable copy-semantic by =deleteing them as:

      Message(Message const &) = delete;
      Message& operator=(Message const &) = delete;
      

      Note that in this case you’ve to implement the move-semantic yourself. Move usually makes sense, so I wouldn’t comment on that.

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently looking at changing from using css to LESS in my current
I try to write Activator action for changing current song rating. I now that
How do you detect the current SCAN state and revert it after changing it?
When changing the VCS for my project FakeItEasy from SVN to Mercurial on Google
After changing the import as a from-import i'm running into this error: from datetime
I am changing the current product name of my iOS application in xcode to
I am changing the current theme of my blog and I wish to add
I'm researching changing my current strategy for creating marketing websites. Currently, I build sites
I'm trying to rename a lot of files in s3--changing the current paperclip has_attached_file
Can anyone please help. I am geting current date from Calendar. I then would

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.