6

I'm studying the C++ programming language and I'm reading the chapter about assignment operator ( = ). In C++ initalization and assignment are operation so similar that we can use the same notation.

But my question is : when i initialize a variable am I doing it with the assignment operator ? When i assign to a variable, am I doing it with the assignment operator ? I think that the only difference is between initialization and assignment because when we initalize a variable we are giving it a new value with the assignmnet operator, when we assign to a variable we are replacing the old value of that variable with a new value using the assignment operator. Is it right ?

2
  • 1
    "and I'm reading the chapter about assignment operator" Of which book/tutorial/...? (But that might be irrelevant..) Commented Sep 25, 2014 at 17:18
  • 1
    When you initialize a variable, you generally are calling one of the various constructors (default, copy, explicit, etc). Commented Sep 25, 2014 at 17:21

2 Answers 2

12

You asked

when i initialize a variable am I doing it with the assignment operator ?

and said

when we initialize a variable we are giving it a new value with the assignment operator

But, no, you are not. The symbol = is used for both copy-initialization and assignment, but initialization does NOT use the assignment operator. Initialization of a variable actually uses a constructor.

In copy-initialization, it uses a copy constructor.

type x = e; // NOT an assignment operator

First e is converted to type, creating a temporary variable, and then type::type(const type&) initializes x by copying that temporary. type::operator=(const type&) is not called at all.

There is also direct initialization, which does not use the = symbol:

type x(e);
type x{e}; // since C++11
otherclass::otherclass() : x(e) {} // initialization of member variable

While both initialization and assignment give a variable a value, the two do not use the same code to do it.


Further details: With C++11 and later, if there is a move constructor, copy initialization will use it instead, because the result of the conversion is a temporary. Also, in copy-initialization, the compiler is permitted to skip actually calling the copy or move constructor, it can convert the initializer directly into the variable. But it still must check that the copy or move constructor exists and is accessible. And copy constructors can also take a non-const reference. So it might be type::type(type&&) that is used, or type::type(const type&&) (very uncommon), or type::type(type&) that is used, instead of type::type(const type&). What I described above is the most common case.

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

8 Comments

"First e is converted to type" is not always true, e could be an argument to another non-explicit constructor with 1 parameter.
@E_net4: That's conversion. A single argument constructor is called a converting constructor.
All right, I stand corrected. My line of thought was way too practical.
@BenVoigt sorry I'm a beginner, can you explain me in a very very simple way ? I just know basic of variables and reading from the keyboard and printing on the screen, Thank
@PieroBorrelli: The simple explanation is, when "=" appears right after the variable name in a variable definition, it is not the assignment operator. It introduces an initializer, which gets processed by a constructor, not by an assignment operation.
|
0

Your thought are quite correct. With a constructor you are allocating a new object of that type, which has its own overhead.

When you copy assign to a variable, instead, you don't want to create a new object since you already have one. What you just need is to set the corresponding member variables of the left hand side of the assignment object to the right size.

For example:

MyClass object = MyClass(10); // here the constructor is called
MyClass other = MyClass(5); // here another constructor is called
object = other // here the copy assignment operator is called, you don't need to build any new object, just setting fields

In addition there is another situation which invokes a copy constructor which is the following:

MyClass other2 = MyClass(object);

Here a special constructor is called which basically does the same work of a copy assignment operation but with a new object, instead that an existing one.

1 Comment

IMO you should make a clearer distinction among 'copy constructor' and 'copy assignment', along with the fact that the use of = does not always translate to the assignment operation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.