0

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

MSDN Entry of the solution from Jason R

Community
  • 1
  • 1
Saltus
  • 21
  • 1
  • 5
  • 1
    "But of course my compiler won't allow me to set them in the constructor because they are in fact const..." Of course it will. Just make sure you *initialize* them, not assign to them. – juanchopanza Aug 13 '15 at 16:33
  • Can you explain *why* [this question](http://stackoverflow.com/q/14495536/3953764) does not answer yours ? – Piotr Skotnicki Aug 13 '15 at 16:34
  • @Piotr Skotnicki Because he wants to initialize the const with a set value (in his case the 100) - I for myself do not know in my program what value it will have – Saltus Aug 13 '15 at 17:31
  • @Saltus replace `100` with a constructor parameter name – Piotr Skotnicki Aug 13 '15 at 17:33
  • @Piotr well, yes. Thanks. If I would have known before that it could be so "easy" I wouldn't have asked the question, you know... – Saltus Aug 13 '15 at 17:52

1 Answers1

7

Use an initializer list in the constructor.

class myRectangle {
private:
   const int length;  //const mainly for "read-only" like
   const int breadth;  //protection
public:
   myRectangle(int init_length, int init_breadth) : 
       length(init_length), breadth(init_breadth) 
   { 
       // rest of constructor body can go here; `length` and `breadth`
       // are already initialized by the time you get here
   }
   int calcArea(void);
   void turn90Degrees(void);
};
Jason R
  • 11,159
  • 6
  • 50
  • 81
  • Thank you for the fast response. Somehow I need to get used to the stackoverflow search function better. Now twice soon after I post my question that bothers me for days I end up finding an answer to it. Because I wasn't aware of the term "delegated constructors" and never heard of an initializer list in that regard I did not know what to look for. If you search for a "bicycle with an engine" you somehow appear to have a hard time finding a "motorcycle" ... – Saltus Aug 13 '15 at 17:29
  • 1
    But as for your answer: Is it possible to declare constructor body outside of the class declaration? And if so how would it look like? – Saltus Aug 13 '15 at 17:33
  • 1
    @Saltus `myRectangle::myRectangle(int init_length, int init_breadth) : length(init_length), breadth(init_breadth) {}` – Post Self Apr 19 '17 at 13:13