HelloWorld
HelloWorld

Reputation: 3

C++ initialization list

Below is code on initialization lists that I do not entirely understand. In particular it is the very last piece in the page (red(Bow("red")) and blue(Bow("blue")).

Bow is another class included in a .h file with a constructor of the form Bow(string aColor).

The initialization syntax is

ClassName(argumentlist): datamember1(value1), dataMember2(value2){}

I do not understand how this initialization works. I understand making an object of class Bow in the class ArcheryCompetition, it is almost as if the constructor of another class is being called in the initialization list of another constructor. This is all from a beginner book I am reading.

If any more clarification is needed please let me know.

class ArcheryCompetition
{
//member variables

private:
    //variables
    int rounds;
    float redScore;
    Bow red;

    float blueScore;
    Bow blue;

public:
    //constructor
    ArcheryCompetition( int lrounds);
    //destructor
    ~ArcheryCompetition();

    //methods
    int compete(void);

};

ArcheryCompetition::ArcheryCompetition(int lrounds):
rounds(lrounds), red(Bow("red")), blue(Bow("blue")), redScore(0), blueScore(0)**
{
}

Upvotes: 0

Views: 504

Answers (6)

kfsone
kfsone

Reputation: 24269

Walk thru what the following do.

Bow("red");

Bow red("red");

Bow red = Bow("red");

Bow redbow("red"); Bow red(redbow);

If you get these but are still struggling with

red(Bow("red"))

then this would be a good time to learn not to give member variables ambiguous names that don't distinguish them from local variables, e.g.

class ArcheryCompetition
{
...
    int m_rounds;
    Bow m_red;
    Bow m_blue;
...

    ArcheryCompetition(int rounds_) // distinguish parameters from local variables too
        : m_rounds(rounds_)
        , m_red(Bow("red"))
        , m_blue(Bow("blue"))
        ...
    {}
...
};

After a Constructor declaration, the ":" syntax denotes an "initializer list" where you initialize the members of the class - where initialization means construction. It is basically a way to say "When I am being constructed, this is how my members should be constructed" - so you are exactly right that the constructors of the other classes are being called; they are being called, in-place, on your classes members, to your specification. Thus "m_blue(Bow("Blue"))" tells the compiler to call the Bow constructor with the argument "blue" to initialize the m_blue member during invocations of this constructor.

Except in this case, by adding the class name, what you are actually doing is

Bow redbow("red");
m_red(redbow);

Which creates a temporary Bow objected initialized with the value "red" and then uses Bow's copy constructor to copy the data into m_red.

you could actually simplify (improve) the constructor down to

    ArcheryCompetition(int rounds_) // distinguish parameters from local variables too
        : m_rounds(rounds_)
        , m_red("red")
        , m_blue("blue")
        ...
    {}

EDIT:

When you see

class MyClass {
    int i;
     YourClass m_yourClass1, m_yourClass2;

public:
    MyClass()
      : m_i(10)
      , m_yourClass1( YourClass(1) )
      , m_yourClass2( 2 )
    {}
};

void foo() {
     MyClass myClass;
     ...
}

Then, roughly, that first line of "foo" causes something like this to happen:

MyClass* myClassPtr = make_room_on_stack_for(sizeof(MyClass));

// : m_i(10)
myClassPtr->m_i = 10;

// , m_yourClass1( YourClass(1) )
YourClass* temporary = make_room_on_stack_for(sizeof(YourClass));
temporary->YourClass(1);
if(YourClass has a copy constructor (YourClass::YourClass(const YourClass&))
    myClassPtr->m_yourClass1->YourClass(temporary); // copy constructor
else
    myClassPtr->m_yourClass1->operator=(temporary); // assignment operator
delete temporary;

// , m_yourClass2( 2 )
myClassPtr->m_yourClass2->YourClass(2);

Upvotes: 0

The code

 ArcheryCompetition::ArcheryCompetition(int lrounds)
    : rounds(lrounds), red(Bow("red")), blue(Bow("blue")), 
      redScore(0), blueScore(0)
 {
 }

is defining a constructor for ArcheryCompetition which sets the member variable rounds to lround, call the Bow constructor with "red" and sets the red member variable (to a copy, so using the copy constructor), etc..

And your terminology is a bit confusing: the C++11 standard gives you initializer lists which are quite different...

Upvotes: 0

undercover
undercover

Reputation: 176

Since members red and blue are both instances of class Bow it will be sufficient to call red("red") and blue("blue"). It will call the constructors of class Bow with chosen arguments:

ArcheryCompetition::ArcheryCompetition(int lrounds):
rounds(lrounds), red("red"), blue("blue"), redScore(0), blueScore(0)
{
}

red(Bow("red")) is actually a call to copy constructor of class Bow.

Bow(const Bow& toCopy); 

It creates temporary instance of Bow, calls its constructor with "red" argument and copies this temporary object byte-by-byte to memory reserved for red member. I know it can be a little bit confusing and I don't know why such constructions are placed in book without explanation what copy constructor is.

Here you can find some good explanation: http://www.cplusplus.com/articles/y8hv0pDG/

Upvotes: 3

Sayalic
Sayalic

Reputation: 7530

Bow("red") will give you a instance of class Bow.

red(Bow("red")) means initializes date member red with Bow("red")

Upvotes: 0

Beta
Beta

Reputation: 99172

The initializer

red(Bow("red"))

calls the Bow constructor with argument "red", then initializes red with that new (anonymous) Bow.

Upvotes: 1

Ed Heal
Ed Heal

Reputation: 60037

For example

Bow("red")

is calling the Bow constructor.

Also you should put the initializers in the same order as the declaration.

Upvotes: 1

Related Questions