syntax misunderstanding player::player() : <--

Hello,
I am new to c++ and I'm reading some code to try to improve my own.
Actually I am facing a syntax I haven't see anywhere before:

In this example Player is a class derivated from the class AnimatedSprite

In the cpp file the default constructor is left empty, in the second constructor with arguments there is a coma I don't understand.

1
2
3
4
5
6
7
8
Player::Player() {} // <---empty constructor

Player::Player(Graphics &graphics, float x,float y): //<-- this ":" I don't understand
AnimatedSprite(graphics,myChar.png,0,0,0, x, y), //<-- this "," I don't understand either
{
   //class constructor implementation
}

Can you tell me what s the name of that syntax so I can look it up and understand it better?
Thank you very much

Mike
Thank you very much!

but what is the difference with what I used to do:

1
2
3
4
5
6
7
Player::Player {}

Player::Player(Graphics &graphics, float x, foat y)
{
   AnimatedSprite::AnimatedSprite(graphics,myChar.png,0,0,0, x, y);
  //class constructor implementation
}


Is this just to make the coding (slightly) shorter?
What you used to doesn't do what you think it does. It doesn't initialize the AnimatedSprite component of itself with the values passed into Player::Player. Instead, it:

1) Uses the default AnimatedSprite constructor to initialize itself.

2) invokes the constructor for an unrelated, un-named, temporary AnimatedSprite object, using the values passed in.
Last edited on
Hi,

Not sure if mutexe is still on-line, hopefully he won't mind if I answer.

The definition of when an object is deemed to have been created, is when a constructor completes. If this doesn't happen, it is as if the object never existed.

By using an in constructor parameter initialiser list (not the same thing as std::initializer_list), the member variables are initialised before the object is created, using the arguments provided.

If one doesn't do this, the object is given default initialisation values (, "" etc) via a possibly implicit default constructor (including base class ctors), then one has to re-initialise the values by assignment or calling an alternative constructor again. This could get expensive if the objects are large.

Also, it is possible to trap exceptions in the constructor parameter initialiser list, by using a function try block.

http://www.drdobbs.com/introduction-to-function-try-blocks/184401297
http://en.cppreference.com/w/cpp/language/function-try-block


So this is handy if any of the arguments throw.

When initialising your class members, make sure you do all of them, and in the same order as in the class definition.

Now we get down to validation: none of the arguments threw an exception, but we want to check valid ranges for the values, or any interdependency between the values, so we can satisfy the invariants. I find it easy to do this in the body of the constructor (or call a private function to do it, from there) and throw if there are problems.

Hopefully this has been helpful :+)
I don't mind at all :)
I'm not sure why you think I would :o
Last edited on
Well, thank you all for those precious informations, I think it's an early stage for me to understand all those subtilities but I m sure it will come handy in a near future :)

Topic archived. No new replies allowed.