0

I'm learning c++ and I was looking at the following webpage:

http://msdn.microsoft.com/en-us/library/vstudio/bb384842.aspx

The code given on that page contains the following line of code under step 7.

Cardgame::Cardgame(int players)
    : players(players)
{
    totalParticipants += players;
    cout << players << " players have started a new game.  There are now "
          << totalParticipants << " players in total." << endl;
}

As I understand it, the first line corresponds to an object constructor which takes a single integer argument. However, I don't understand the line directly after it

: players(players)

What does this notation mean? Could someone please explain this to me in as simple a way as possible?

covertbob
  • 691
  • 2
  • 8
  • 15

4 Answers4

1

It is called member initialization list. You may find this thread C++ member initialization list useful.

In the code you posted, it will initialize the class member players with the input value players. (BTW, bad style to use the same names).

Community
  • 1
  • 1
taocp
  • 23,276
  • 10
  • 49
  • 62
1

It is called the member-initialization-list.

It is used to initialize members of a class by choosing the best initializer for it. Here you are initializing your member players with the parameter of the same. (It can be confusing to choose the same name, it is not a really good practice.)

From the standard :

12.6.2 Initializing bases and members [class.base.init]

In the definition of a constructor for a class, initializers for direct and virtual base subobjects and non-static data members can be specified by a ctor-initializer, which has the form

ctor-initializer:
    mem-initializer-list

mem-initializer-list:
    mem-initializer ...opt
    mem-initializer , mem-initializer-list ...opt

mem-initializer:
    mem-initializer-id ( expression-listopt)
    mem-initializer-id braced-init-list

mem-initializer-id:
    class-or-decltype
    identifier

I really suggest you to read the first post of this thread who explain it very well by quoting Scott Meyers : http://www.cplusplus.com/forum/articles/17820/

You can get more informations here :http://en.cppreference.com/w/cpp/language/initializer_list

Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62
0

This refers to initialization list which is another form of initialising the members of an object.

Advantage of initialization list over normal construction is that it usually fast as it doesn't create a temporary object before initializing the members.

And a disadvantage could be that this pointer is to be used a bit carefully in initialization list

Saksham
  • 9,037
  • 7
  • 45
  • 73
0

As it's already mentioned by taocp, the line refers to member initialization list.

There are couple of ways to initialize members 1. member initialization list (efficient approach) 2. using assignment e.g. players = 10

It might not make any difference for built-in types e.g. int, char but if you are assigning big objects then use member initialization list. Constructor/ Destructor gets called in assignment which is definitely not warranted

HVar
  • 120
  • 8