0

Saw this code in my project codebase. Wanted to know whats exactly happening here with the variables. Is this some kind of initialization? If yes whats the point of doing this way ? This is not inside any function. Here are my queries for the below mentioned code:

  1. Why are they declared in such a manner?
  2. Why does the variables var1,2,3 have (0).
  3. What role does the colon play here ?
  4. Why are only integers placed after colon: but the curly braces section have both integer and chars? Can only integers be placed in the colon area? why not everything inside the curly braces.
  5. I observed in GitHistory that the order of the variables were changed (and nothing else), does the sequence have any relevance? Does this need to match the order in which they are declared in the class? Trying to understand whats happening here?

 ClssDoAction :: ClssDoAction() :
 var1(0),     //declared as int
 var2(0),     //declared as int
 var3(0)      //declared as int
 {<br/>
  var4[0] = 0;  //declared as char
  var5[5] = 0;  //declared as char
  var6 = 128;   //declared as int
  var7 = NULL   //declared as char
 }

  ClssDoAction::~ClssDoAction ()
  {  }
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 1
    1+2+4 only the author knows. 3: https://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor 5: members are initialized in the order they appear in the class definition not in the order of the initializer list, compilers warn when order is different. Please one question per question – 463035818_is_not_an_ai Apr 25 '23 at 07:27
  • This is several questions in one. #2 and #3 are addressed by [this dupe](https://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor). #5 is addressed [here](https://stackoverflow.com/questions/40633206/c-class-member-initialization-sequence). #1 and #4 I cannot answer without knowing more about the context of the original code. – Nathan Pierson Apr 25 '23 at 07:28
  • 1
    btw this constructor is not parametrized, it has no parameters – 463035818_is_not_an_ai Apr 25 '23 at 07:30
  • The colon is the start of an *initialiser list*. It makes me weep when newbies ask about this. If you are learning C++ from a decent source initialiser lists will be introduced very early on when you learn about constructors. The unfortunate fact is that many newbies are trying to pick up C++ as they go along. It's never going to get you very far, C++ needs proper study. – john Apr 25 '23 at 07:34

1 Answers1

2

Much of your astonishment can be resolved with this almost-dupe What is this weird colon-member (" : ") syntax in the constructor? and clarifies what is a Member Initializer List (what lies between the colon and the opening-curly-brace).

Now...

  1. Why are they declared in such a manner?

Member Initializer List have a multitude of advantages over assignation in the constructor body (between the curly-braces), one of witch being that the member are directly initialised with the intended value and not initialised and then assigned to.

  1. Why does the variables var1,2,3 have (0).

This is part of the syntax. This makes those variable being directly initialised with the value 0 upon construction.

  1. What role does the colon play here ?

This is part of the syntax, you know that now.

  1. Why are only integers placed after colon: but the curly braces section have both integer and chars? Can only integers be placed in the colon area? why not everything inside the curly braces.

Other types are possible, but var4[0] = 0 is a good example of what is not possible with a Member Initializer List. On the other hand, var7 = NULL could (should) be turned into car7(NULL) in the Member Initializer List.

  1. I observed in GitHistory that the order of the variables were changed (and nothing else), does the sequence have any relevance? Does this need to match the order in which they are declared in the class? Trying to understand whats happening here?

The order matters only to the human reader. Member variables will be initialised in the order they are defined in the class, irrespective of the order given in the initialiser list. Yet most compilers I know will warn you if you list them in the wrong order; the modification you are mentioning could have been motivated by such a warning.

YSC
  • 38,212
  • 9
  • 96
  • 149
  • 2
    Last sentence could be confusing, how about - member variables will be initialised in the order they are defined in the class, irrespective of the order given in the initialiser list. – john Apr 25 '23 at 07:40
  • 1
    Indeed, if anything the order in the actual constructor definition very much _doesn't_ matter. There's an order that matches the order they're actually initialized in, and many orders that mislead readers, but from the perspective of the compiler they're the same. – Nathan Pierson Apr 25 '23 at 07:43
  • @YSC I don't think that is the case. – john Apr 25 '23 at 07:44