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?
Use a static member function the compute to result you need and call that function in the initialization list. Like this:
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
thispointer. That means it needs to be a static member function.Now, the type of
myVec.size()isstd::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 anintwhich 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.