• Advertisement
Sign in to follow this  

Question on struct

This topic is 3645 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

struct abc {
  int x, y;
  abc(int x, int y): x(x), y(y) {}
};

What is the meaning of " abc(int x, int y): x(x), y(y) {}"?

Share this post


Link to post
Share on other sites
Advertisement
That's called a member initialization list. When that constructor is called, the x and y members of that abc struct instance will be intialized with the values of the x and y parameters sent to the constructor. It's not particularly useful for primitive types like ints, and is usually used for objects that are members of a struct or class.

Share this post


Link to post
Share on other sites
The 'pro' of using initializer lists should be obvious: It's faster to write than the actual constructor list(ie, { this->x = x; this->y = y; }), and it also garantuees that before executing ANY code in the constructor, the variables will be set to a value.

If you use the initializer lists properly and update them when you add in new class variables, it could help prevent bugs related to uninitialized values.

Toolmaker

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement