Braces or Parenthesis?

When I passed in the window dimensions I expected to do it with parenthesis.

Character Knight(WindowDimensions[0], WindowDimensions[1]);

This ran and worked.
But when he did it, he used curly braces instead.

Character Knight{WindowDimensions[0], WindowDimensions[1]};

Can anyone offer more clarity here?

2 Likes

The top example looks like a method and the bottom example looks something like an object literal, if memory serves.

2 Likes

Because in the end, the braced initialization (bottom) actually calls the Constructor (top) in the background.

Both end up creating and initializing the Knight as a Character object. Braced initialization has the added benefit of forcing variables to be initialized to default values. Though when it comes to classes the benefit is not really seen.

3 Likes

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms