quantum231
quantum231

Reputation: 2585

How were const and reference types initialized before initializer lists?

The Ivor Horton's Beginning Visual C++ 2013 states about "initializing lists" in C++ that:

"With class members that are const or reference types, you have no choice as to how they are initialized. The only way is to use a member initializer list in a constructor. Assignment within the body of a constructor will not work."

My Visual Studio 2012 express did not compile the initializer list so I was confused at first, then I realized that it is not supported in it.

My question is how did people initialize const or reference types before initializer lists since the book says that there is no other way besides using initializer lists?

Upvotes: 0

Views: 60

Answers (2)

Nicol Bolas
Nicol Bolas

Reputation: 473272

You're confusing two separate terms. That's OK; they're named almost identically.

There are initializer lists (which I call "braced-init-lists" precisely to avoid this confusion). These are {} delimited sequences of items which are used to initialize values. If the items are of the same type, they can be converted into a std::intializer_list. This was a C++11 addition.

Then, there is the member initialization list in a constructor. That's this syntax:

TypeName(params)
  : member1(...)
  , member2(...)
{
  /*constructor code*/
}

That's been in C++ since C++98/03. The member initialization list is how you initialize const members and reference types.

Upvotes: 4

Baum mit Augen
Baum mit Augen

Reputation: 50053

The Horton quote talks about the Member initialization list which is a C++98 feature.

This is unrelated to std::initializer_list and list initialization with the type name {stuff...}; syntax which both are indeed C++11 features.

Upvotes: 1

Related Questions