10

I'm reading c++ primer 4th edition by Stanley Lipmann and I'm on page 92 about value initialisation.

I just don't understand when about value initialisation. I browsed around and I know there are also default initialisation and zero-initialisation. Can anyone explain about value initialisation?

Coming to this paragraph ..

clause a)

"Some classes does not define default constructor. We cannot initialise vector of such a type by specifying only a size, we must also specify an initial value"

I do understand the above but I find that the below contradict the above sentence.

clause b)

"Element type might be of a class type that does not define any constructors. In this case, the library still creates a value-initialised object. It does so by value-initialising each member of that object"

I don't understand the clause b.

Any help is appreciated

2 Answers 2

8

a) This is true, if the class defines other constructors - thereby suppressing generation of a default constructor.

struct Foo { Foo(int n) : mem(n) {} int mem; }; 

This class can't be value-initialized.

b) If the class has no constructors defined, value-initialization will simply value-initialize all sub-objects (base classes and non-static members)

struct Foo { Foo() : mem(0) {} int mem; }; struct Bar { Foo f; }; 

Value-initialization of Bar simply means that the f member will be value-initialized.

See e.g. What do the following phrases mean in C++: zero-, default- and value-initialization?

Sign up to request clarification or add additional context in comments.

4 Comments

Can you give me some example for clause b)?
@yapmk01: Updated answer with some samples
I'm new to C++. I'm from Java background. The clause b) you mentioned Foo() : mem(0) {} .. Isn't Foo() a default constructor? In Java, a default constructor is one without any parameters. Can C++ class have NO constructor at all (meaning no default constructor too)? From what i understand .. if you don't supply any constructor, the compiler will generate the default one for you. If C++ class can have NO constructor at all, how do you initialise it?
@yapkm01: If it has no constructor, an implicit parameter-less constructor is generated
0
#include <vector> #include <string> class fooz { private: string s; int n; public: fooz(string& str, int num) { s=str; n=num; } ~fooz(){} void gets(string& str) {str=s;} void getn(int& num) {num=n;} }; vector<class fooz> vfDialpad = { fooz(string(""),0), fooz(string(""),1), fooz(string("abc"),2), fooz(string("def"),3), fooz(string("ghi"),4), fooz(string("jkl"),5), fooz(string("mno"),6), fooz(string("pqrs"),7), fooz(string("tuv"),8), fooz(string("wxyz"),9) }; 

after this, both rules have been activated. this is an initializer list and it is new to C++, you may or may not find it only in newer versions of gcc. each element gets the new object initialized.

maybe I am not making the best example here, but it's a start.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.