Search code examples
c++objectconstructorsdlsdl-2

Confusing syntax when passing in an object to a constructor SDL2.0 C++


So this is a different question in the sense that my code is working but I'm not sure what its doing, I am following a tutorial on getting used to rendering in SDL 2.0.

I have the following constructor for initialising member values of a rectangle to be rendered:

rect::rect(const Window &window, int otherValues):Window(window), _memberValues(otherValues)

When a rect object is made I pass in a window object and the initialisation values into the constructor, looking like this:

rect ro(window,someNumbers);

My question is that I don't understand what is meant by

const Window &window

and

Window(window)

or what they exactly mean. I know how to initialise members and pass in values to constructors, but I don't know whats happening in the context of my window object.

Any explanations would be appreciated.


Solution

  • const Window &window is a constant (const) reference (&) named window of type Window.

    The const keyword indicates that it is not to be modified and the & indicates that it is passed by reference rather than by value (I.E. it is not copied).

    Window(window) is a call to Window's copy constructor with the same aforementioned window passed in as the parameter. Presumably rect inherits from Window and so in order for rect to be constructed its Window must also be constructed.