Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ value initialization

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

like image 341
yapkm01 Avatar asked Apr 17 '11 23:04

yapkm01


People also ask

What is an initialization value?

In computer programming, initialization (or initialisation) is the assignment of an initial value for a data object or variable. The manner in which initialization is performed depends on the programming language, as well as the type, storage class, etc., of an object to be initialized.

What is initialization in C?

Initialization is the process of locating and using the defined values for variable data that is used by a computer program. For example, an operating system or application program is installed with default or user-specified values that determine certain aspects of how the system or program is to function.

How do you initialize a value?

More precisely, do the following to initial a variable with the value of an expression: add an equal sign (=) to the right of a variable name. to the right of the equal sign, write an expression. It is important to note that all names in the expression must constants or names of constants.

Does C automatically initialize variables?

Unlike some programming languages, C/C++ does not initialize most variables to a given value (such as zero) automatically. Thus when a variable is given a memory address to use to store data, the default value of that variable is whatever (garbage) value happens to already be in that memory address!


1 Answers

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?

like image 142
Erik Avatar answered Oct 08 '22 23:10

Erik