Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ object creation and constructor

I'm learning ctors now and have a few question. At these lines:

Foo obj(args);

Foo obj2;
obj2 = Foo(args);

Foo obj3 = Foo(args);

First part: only 1 constructor called (Foo) and obj is initialized. So, 1 object creation.

Second part: creating of temporary object obj2, calling default ctor for it. Next lines we create another copy of Foo and pass its copy into operator=(). Is that right? So, 3 local temporary objects, 2 constructor callings.

Third part: create 1 object Foo and pass its copy into operator=(). So, 2 temprorary objects and 1 ctor calling.

Do I understand this right? And if it's true, will compiler (last gcc, for example) optimize these in common cases?

like image 795
Max Frai Avatar asked Jan 08 '12 11:01

Max Frai


People also ask

Is object created by constructor?

Simply speaking, a constructor does not create an object. It just initializes the state of the object. It's the new operator which creates the object.

Can we create constructor in C?

A Constructor in C is used in the memory management of C++programming. It allows built-in data types like int, float and user-defined data types such as class. Constructor in Object-oriented programming initializes the variable of a user-defined data type. Constructor helps in the creation of an object.

Why is constructor important in creating objects?

There are the following reasons to use constructors: We use constructors to initialize the object with the default or initial state. The default values for primitives may not be what are you looking for. Another reason to use constructor is that it informs about dependencies.

What is object and constructor?

Object() constructorThe Object constructor turns the input into an object. Its behavior depends on the input's type. If the value is null or undefined , it creates and returns an empty object. Otherwise, it returns an object of a Type that corresponds to the given value.


2 Answers

I will comment on the third one first:

Foo obj3=Foo(args);

It doesn't use operator= which is called copy-assignment. Instead it invokes copy-constructor (theoretically). There is no assignment here. So theoretically, there is two objects creation, one is temporary and other is obj3. The compiler might optimize the code, eliding the temporary object creation completely.

Now, the second one:

Foo obj2;         //one object creation
obj = Foo(args);  //a temporary object creation on the RHS

Here the first line creates an object, calling the default constructor. Then it calls operator= passing the temporary object created out of the expression Foo(args). So there is two objects only the operator= takes the argument by const reference (which is what it should do).

And regarding the first one, you're right.

like image 122
Nawaz Avatar answered Oct 26 '22 15:10

Nawaz


  1. Yes, Foo obj(args) creates one Foo object and calls the ctor once.

  2. obj2 is not considered a temporary object. But just like 1 Foo obj2 creates one object and calls the Foo ctor. Assuming that you meant obj2 = Foo(args) for the next line, this line creates one temporary Foo object and then calls obj2.operator=(). So for this second example there is only a single temporary object, a single non-temporary, Foo ctors are called twice (once for the non-temporary, once for the temporary) and the operator=() is called once.

  3. No, this line does not call operator=(). When you initialize obj3 using the = syntax it is almost exactly as if you had used parentheses instead: Foo obj3(Foo(args)); So this line creates a temporary object, and then calls the Foo copy ctor to initialize obj3 using that temporary object.

like image 45
bames53 Avatar answered Oct 26 '22 15:10

bames53