Well this might question might have a fair amount of overlap with these questions:
how to initialize const member variable in a class C++
Const Member Variables in C++11
Initializing a const member variable after object construction
Yet, none of these could answer my original question. The main difference being that I do not want to initialize with a set value but with a constructor parameter.
What I want to do is something like this-
class myRectangle {
private:
const int length; //const mainly for "read-only" like
const int breadth; //protection
public:
myRectangle(int init_length, int init_breadth);
int calcArea(void);
void turn90Degrees(void);
/* and so on*/
}
where both length and breadth are kinda read-only protected from altering them after construction.
But of course my compiler won't allow me to set them in the constructor because they are in fact const...
I did come up with the workaround to just leave them variable and only implement getter methods so they cannot effectively be changed but I pretty much feel like I am missing the obvious solution here.
Also I feel like I misunderstood the use of const to some degree. So is it "already" a compile-time contract to not alter the data from there on? Because in my understanding isn't knowing the size the constant takes in the execution of the program enough information?
And btw the solution of making the constant static would not fit for me because every rectangle I spawn should have a different size.
Thank you for your answers and clarifications!
Solution: Intializer Lists / delegating constructors