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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:41:30+00:00 2026-05-13T20:41:30+00:00

First, my latest coding is Java, and I do not want to write Java

  • 0

First, my latest coding is Java, and I do not want to “write Java in C++”.

Here’s the deal, I have to create an immutable class. It’s fairly simple. The only issue is that getting the initial values is some work. So I cannot simply call initializes to initialize my members.

So what’s the best way of creating such a class? And how can I expose my immutable / final properties to the outside world in C++ standards?

here’s a sample class:

class Msg {
    private:
        int _rec_num;
        int _seq;
        string text;
    public:
        Msg(const char* buffer) {
            // parse the buffer and get our member here...
            // ... lots of code
        }

        // does this look like proper C++?
        int get_rec_num() { return _rec_num; }
    };
  • 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-05-13T20:41:30+00:00Added an answer on May 13, 2026 at 8:41 pm

    C++ offers some nice mechanisms to make your class immutable. What you must do is:

    • declare all your public (and maybe protected) methods const
    • declare (but not define) operator= as private

    This will ensure that your objects cannot be modified after they have been created. Now, you can provide access to your now immutable data members anyway you want, using const methods. Your example looks right, provided that you make it const:

    int get_rec_num() const { return _rec_num; }
    

    EDIT: Since C++11 you can explicitly delete operator=, rather than just leave it undefined. This explicitly instructs the compiler to not define a default copy assignment operator:

    Msg& operator=(const Msg&) = delete;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

my first time posting. I have a tricky task of finding the latest date
It's not a real programming (coding) problem, sorry for that (but design comes first).
I want to show the three latest posts with the 50 first characters. And
I am trying to do a sort here to display the latest entries first.
My sandbox is here: http://9.latest.truxmap.appspot.com/ First click a marker on the map (if there
The code i current have is this. function update (){ latest_id = $('#image:first').data('position'); /*
first take a look on this picture from localScope app : i have 2
first off I'm a noob to PHP but here is my problem. I am
First off, I'm coming (back) to Java from C#, so apologies if my terminology
First you should know I have looked into many questions and none of them

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.