0

Possible Duplicate:
What is this weird colon-member syntax in the constructor?
C++ initialization

I have just received a header file in a C++ program, and I cannot figure out what this line of code does:

Card(Value faceValue=deuce, Suit suit = clubs):
suit(suit), faceValue(faceValue) {}

What does the : mean, and why does replacing it with a ; (as I thought I should) break the code?

Sorry for the generalness of this question, but could someone please explain the purpose of these two lines?

Thank you for your time.

Community
  • 1
  • 1
Oliver Spryn
  • 16,871
  • 33
  • 101
  • 195

3 Answers3

2

This looks like a constructor for the Card class. The part after the : is an initializer list, initializing the values of member variables (or parent classes, but I don't think that's applicable in this case). The body of the constructor is empty because everything it needed to do was done in the initializer list.

Praetorian
  • 106,671
  • 19
  • 240
  • 328
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • Hmm... could you elaborate a tad on initializer list? – Oliver Spryn Sep 17 '12 at 03:21
  • 2
    @spryno724 Read [this](http://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor) – Praetorian Sep 17 '12 at 03:21
  • 1
    @PhilipWhitehouse There's a pretty big difference between initializing a member in the initialization list and doing it within the body of the constructor. Doing the latter means that the object is first default constructed, and then assigned some value within the body. – Praetorian Sep 17 '12 at 03:26
  • @Prætorian Is there an expanded alternative to the initialization list? – Oliver Spryn Sep 17 '12 at 03:27
  • 1
    @spryno724 What do you mean by *expanded alternative*? – Praetorian Sep 17 '12 at 03:28
  • @Prætorian I see that the previous edit of this answer was removed. It showed what the editor claimed to be an alternative to two lines of code provided in the question. Was that indeed an alternative "expanded" method for writing the code appearing in my question? – Oliver Spryn Sep 17 '12 at 03:30
  • 1
    @spryno724, What that alternative was doing was assigning values *inside* the body, not initializing them. There are some catches to what gets initialized before the body, but it's better to do it there, and good luck initializing `const` or reference members otherwise. – chris Sep 17 '12 at 03:31
  • @Prætorian I stand corrected, thanks for improving my C++ :). – Philip Whitehouse Sep 17 '12 at 03:32
  • 1
    @spryno724 I explained the difference in my comment above. The penalty (if you can call it that) is trivial in case of primitive types, but it can be significant if your member object performs some expensive operation during default construction. Also, the initializer list is the only option for initializing `const` and reference members of your class. – Praetorian Sep 17 '12 at 03:33
  • @spryno724, the edit (from somebody else) showed a naive alternative where the values were assigned rather than initialized. Unfortunately it didn't explain the differences which are subtle but important. – Mark Ransom Sep 17 '12 at 03:35
  • @Prætorian Thank you for filling me in. – Oliver Spryn Sep 17 '12 at 03:35
  • @Prætorian, I believe built in types aren't initialized in the list depending on how you create the object. Also, as another side note, make sure the order in the list matches the order of declaration in the class. – chris Sep 17 '12 at 03:35
  • 1
    @chris good advice. I think the initialization order follows the declaration order within the class rather than the order in the initialization list, but I can never remember for sure - best to make them both the same. – Mark Ransom Sep 17 '12 at 03:38
  • @MarkRansom, You're correct. If your class is `struct S {MyType a; MyType b;};` and you have `S() : b(), a(){}`, you've effectively initialized `a` twice. It's even more important when you have something like `int x; int y; int z;` and `S() : z(5), x(z), y(z){}`, though, as `z` is still uninitialized when you use it to initialize `x` and `y`. GCC does warn about this: *In constructor 'S::S()': warning: 'S::z' will be initialized after ... 'int S::x' ... when initialized here* – chris Sep 17 '12 at 03:42
2

The : and what follow is the initialization list. The reason you use it instead of assigning the member variables in the constructor's body is that if you do it inside the constructors body, the default constructor will be called first and then the copy constructor or assignment operator will be called afterwards. By using the initialization list you skip the first step.

Borgleader
  • 15,826
  • 5
  • 46
  • 62
1

Initialization lists. It's the preferred way to initialize class constructors in C++.

It is used because it allows the initialization of const members of the class without compilation error.

Rapptz
  • 20,807
  • 5
  • 72
  • 86