Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "member initializer" in C++11?

I run across a weird concept named "member initializer".

Here says:

C++11 added member initializers, expressions to be applied to members at class scope if a constructor did not initialize the member itself.

What is its definition?

Are there some examples to illustrate its usage?

like image 919
xmllmx Avatar asked Sep 15 '13 11:09

xmllmx


1 Answers

It probably refers to in-class member initializers. This allows you to initialize non-static data members at the point of declaration:

struct Foo
{
  explicit Foo(int i) : i(i) {} // x is initialized to 3.1416
  int i = 42;
  double x = 3.1416;
};

More on that in Bjarne Stroustrup's C++11 FAQ.

like image 183
juanchopanza Avatar answered Oct 11 '22 10:10

juanchopanza