Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

direct initialization and copy initialization of reference

Tags:

c++

I have been reading "C++ primer". For the initialization of object, C++ supports 2 forms of initialization: direct and copy. but the book does not refer the initialization of reference. And in the book I have never seen the direct initialize(if exists) of a reference. All is the copy one like:

int i;
int &j = i;//but not int &j(i);which also works in my experiment

I want to know that is it the same that are going on underneath for the initialization of a reference. for the following codes:

string null_book = "9-999-99999-9";

the initialization progress is first create a temporary string object tmp(for instance) that will direct initialized with a c style string parameter, and then initialize the variable null_book with the copy Constructor. That make sense to me. for this one:

int &j = i;

will ref j be initialized the same way? That will be a temp reference it &t(for example) initialized by i and then initialize j with t? that doesnt make sense??? Why the book never use the direct initialization for reference? Thanks for your attention!

like image 997
Joey.Z Avatar asked Dec 19 '12 12:12

Joey.Z


People also ask

What is direct initialization?

Direct Initialization or Assignment Operator (Syntax) This assigns the value of one object to another object both of which are already exists. Copy initialization is used when a new object is created with some existing object. This is used when we want to assign existing object to new object.

What is the difference between copy assignment and copy initialization?

Comparison ChartThe copy constructor initializes the new object with an already existing object. The assignment operator assigns the value of one object to another object both of which are already in existence. (1)Copy constructor invokes when a new object is initialized with existing one.

What is the other name for copy initialization?

A copy constructor is a member function that initializes an object using another object of the same class. In simple terms, a constructor which creates an object by initializing it with an object of the same class, which has been created previously is known as a copy constructor.

How do you initialize a reference variable?

There are three steps to initializing a reference variable from scratch: declaring the reference variable; using the new operator to build an object and create a reference to the object; and. storing the reference in the variable.


2 Answers

The main differences between direct-initialization and copy-initialization are covered in section 8.5, paragraph 17 of the standard. In general, the difference is that for class types in copy-initialization, explicit constructors are not considered (only converting constructors are considered) and a possibly elided copy is made; in direct-initialization explicit constructors are considered and the target is constructed directly. From section 8.5 of the standard:

14 - The form of initialization (using parentheses or =) is generally insignificant, but does matter when the initializer or the entity being initialized has a class type [...]

For non-class types (including references), direct-initialization and copy-initialization have similar semantics; for references, a reference binding occurs in either case, as specified in 8.5.3 References [dcl.init.ref]. Direct-initialization and copy-initialization of a reference only have different semantics where a conversion function is involved (13.3.1.6 Initialization by conversion function for direct reference binding [over.match.ref]); again, direct-initialization is allowed to invoke explicit conversion functions where copy-initialization is not.

So, in

int &j = i;

8.5.3p5 applies and the reference j is bound directly to the lvalue i. No temporaries are invoked.

In terms of complexity, references are closer to fundamental (primitive) types than to class types. Primitives are initialized without a temporary being constructed (8.5p17, last bullet) and in general references are too. This is probably why the book only uses the = form for initialization of references; as with primitives, there is usually no difference and writing int i = x; is usually clearer than int i(x);.

like image 57
ecatmur Avatar answered Sep 19 '22 20:09

ecatmur


The terms copy-initialization and direct-initialization are part of the grammar of C++. they don't immediately tell you what sort of code generation happens. The meaning of any grammatical construction is described by the standard, and the context of initialization there are lots of different particular consequences depending on the types of the things that are being initialized.

In particular, for primitive types, pointer types and reference types, both direct- and copy-initialization (the grammatical construct!) have the exact same effect. That is, fundamental and pointer types are initialized with the value of the initializer, and references are bound to the object referred to by the initializer:

int a1 = 5;
int a2(5);      // same thing

Foo * p1 = &x;
Foo * p2(&x);   // same thing

Bar & b1 = y;
Bar & b2(y);    // same thing

(However, for user-defined types there is a distinction between direct- and copy-initialization, although it is a subtle one that is usually not important.)

like image 45
Kerrek SB Avatar answered Sep 21 '22 20:09

Kerrek SB