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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:51:05+00:00 2026-06-14T13:51:05+00:00

I often find myself wishing I could have an object’s member variable be const,

  • 0

I often find myself wishing I could have an object’s member variable be const, but that the system allowed initialization of that const variable after construction. Is there a mechanism that would allow me to do this?

To clarify, here is an example:

class A
{
public:
    A(){}
    initialize(int x) { c = x; }
private:
    const int c;
}

I want to be able to do something like that. I don’t have this information at construction, so I can’t simply move initialization to the initialization list of the constructor.

  • 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-14T13:51:06+00:00Added an answer on June 14, 2026 at 1:51 pm

    No, you cannot initialize const member after contruction.

    Do not forget, however, that you can call static functions in initializer list, so in most of cases you can initialize memebers from initializer list

    class A
    {
    public:
        A(){}
        initialize(int x):c(computeC(x)) {}
    private:
        const int c;
        static int computeC(int){/*...*/}
    };
    

    You can also define special getter for that member and use it to access member.

    class A
    {
    public:
        A(){}
        initialize(int x) { c_internal = x; }
    private:
        const int& c() const { return c_internal; }
        int c_internal;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I often find myself needing reference to an object that is several objects away,
I often find myself asserting that an object isKindOfClass of some class in Objective-C.
I often find myself wanting to debug CSS layout issues that involve DOM changes
I often find myself with a list of disconnected Linq2Sql objects or keys that
I often find myself with a file that has one number per line. I
While striving for const-correctness, I often find myself writing code such as this class
I often find myself with fairly complex data that represents something that my objects
I often find myself designing simple little web projects that are serving up aggregate
I'm a powershell newbie, but I often find myself starting and stopping a small
I often find myself remembering the name of a class that I want to

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.