Thursday, 15 September 2011

c++11 - Proper ways to initialize member variables with identical names as the parameters -


I get a square that looks like

  class rational {public: Logical (int P = 0, int q = 1): p (p), q (q) {}; Private: int p; Int q; };  

My question is about the initial syntax where the member variables and the constructor parameters are the same names. Now I know that it is legal to do this, but my question is if I want to I'm "clean", so I'm easy to understand if I can do as I normally do in Java:

  // Legal Java code this.q = q ; This p = p; // Is any of these legal C ++ code (if yes, then what)? This.q (q); This.p (p); // or this- & gt; Q (q); This- & gt; P (P);  

Although I have not tested it, and I can test it, I still want to know the C ++ conventions of doing so.

in C ++ , you must say:

< Pre> this - & gt; Q = q; This - & gt; P = P;

or equivalent

  (* this) .q = q; (* this). P = P;  

But I think the member initializer list syntax

  logical (int p = 0, int q = 1): p (p), q ( Q) {}  

Cleaner (note the lack of a semicolon!) Why do not you like it?


No comments:

Post a Comment