Page 92, C++ primer 4th edition

A

asdf

How to understand the last paragraph? Who'd like to give me an example?

There is a third possibility: The element type might be of a class type
that does not define any constructors. In this case, the library still
creates a value-initialized object. It does so by value-initializing
each member of that object.
 
V

Victor Bazarov

asdf said:
How to understand the last paragraph? Who'd like to give me an
example?

There is a third possibility: The element type might be of a class
type that does not define any constructors. In this case, the library
still creates a value-initialized object. It does so by
value-initializing each member of that object.

Is this the paragraph you want to understand?

'Element', eh? Probably talking about arrays. Class that does not
define "any" constructors means that it has an implicitly-declared
and implicitly-defined (hopefully) default constructor (c-tor that
doesn't take any arguments). If it's defined (IOW, if all members
to be initialised *can* be initialised using default-initialisation),
then initialisation of an array [of objects] of such class will make
all elements of the array default-initialised.

class A { int a; public: A() : a() {} };
class B { A a; };

int main() {
B *pb = new B[10](); // ten elements, all value-initialised
}

In the example above, each 'a' of each 'B' will have it's 'a' set
to 0 (value-initialisation).

If this is not what the book is talking about, then you have to
explain what you're asking about, by quoting a bit more of the book.
I don't have a copy.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top