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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:37:32+00:00 2026-06-15T07:37:32+00:00

Usually when you have a constant private member variable in your class, which only

  • 0

Usually when you have a constant private member variable in your class, which only has a getter but no setter, it would look something like this:

// Example.h
class Example {
    public:
        Example(const int value);
        const int getValue() const;
    private:
        const int m_value;
};


// Example.cpp
#include "Example.h"

Example::Example(const int value)
: m_value(value)
{
}

const int Example::getValue() const
{
    return m_value;
}

Now what I’m trying to do, is have a constant int member variable like that, but instead of defining it in the initializing section like so: : m_value(value) I need to take an other object – I’ll use a vector in this example – as the constructor’s parameter, and set m_value based on the parameter object. In this case, I’ll try to do vector’s size + 1, if the size is above 0. So this is what I did:

Example::Example(std::vector<Example*> myVec)
{
    if (myVec.size()) {
        m_value = myVec.size() + 1;
    }
    else {
        m_value = -1;
    }
}

But I get an error uninitialized member 'Example::m_value' with 'const' type 'const int' and if I init m_value inside the initializing section, I get the error assignment of read-only data-member 'Example::m_value' which all makes sense to me, I’m supposed to get those errors, but how could I go around them?

Edit: Only way I could edit m_value is inside the object itself (since m_value is private). Having only getter would limit me from setting m_value to anything other than what it’s set in the constructor. Do I benefit anything from having constant int as a member variable?

  • 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-15T07:37:33+00:00Added an answer on June 15, 2026 at 7:37 am

    Use a static member function the compute to result you need and call that function in the initialization list. Like this:

    // Example.h
    class Example {
        public:
            Example(const int value);
            Example(std::vector<Example*> myVec);
    
            const int getValue() const;
        private:
            const int m_value;
    
            static int compute_m_value(::std::vector<Example*> &myVec);
    };
    
    // Example.cpp
    #include "Example.h"
    
    Example::Example(const int value)
    : m_value(value)
    {
    }
    
    Example::Example(std::vector<Example*> myVec)
    : m_value(compute_m_value(myVec))
    {
    }
    
    const int Example::getValue() const
    {
        return m_value;
    }
    
    int Example::compute_m_value(::std::vector<Example*> &myVec)
    {
        if (myVec.size()) {
            return myVec.size() + 1;
        }
        else {
            return -1;
        }
    }
    

    In this particular case, the function is so very simple you can simply use the ternary operator (aka : m_value(myVec.size() > 0 ? int(myVec.size() + 1) : int(-1)) in the constructor to directly compute the value at initialization. This looked like an example, so I gave you a very general method of solving the problem, even when the method of computing the answer you need might be very complex.

    The general issue is that constant member variables (and member variables that are references too BTW) must be initialized in the initializer list. But initializers can be expressions, which means they can call functions. Since this initialization code is pretty specific to the class, it should be a function private (or maybe protected) to the class. But, since it’s called to create a value before the class is constructed it can’t depend on a class instance to exist, hence no this pointer. That means it needs to be a static member function.

    Now, the type of myVec.size() is std::vector<Example*>::size_t, and that type is unsigned. And you’re using a sentinel value of -1, which isn’t. And you’re storing it in an int which may not be the right size to hold it anyway. If your vector is small, this likely isn’t an issue. But if your vector acquires a size based on external input, or if you don’t know how large it will get, or any number of other factors, this will become an issue. You should be thinking about that and adjusting your code accordingly.

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

Sidebar

Related Questions

It's a known fact that Windows applications usually have 2Gb of private address space
When performing many inserts into a database I would usually have code like this:
In a Python system for which I develop, we usually have this module structure.
I usually have a sas macro code which automatically run macros Save As and
I have two entities that usually have one-to-many relationship, but in rare cases should
Module in macruby has many methods that it doesn't usually have. One of these
I usually have the tw=80 option set when I edit files, especially LaTeX sources.
I usually have my structure laid out something like this: <div id=all> <div id=page>
I usually have to check things like: if ['Bob','Mary','John'].include? @user.name Is there a way
When I run an MSI (without parameters) I usually have to click my way

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.