S
S
sddvxd2018-03-26 21:21:08
C++ / C#
sddvxd, 2018-03-26 21:21:08

What is the order in which a new object is assigned?

Hello, I want to understand this:

Type a;
Type b = a; //Тут вызывается operator= или конструктор копирования?

In the literature that I read, this is called an assignment, but the assignment operator returns a reference to the argument: Does this mean that by manipulating the members of b I will edit the memory of a? If, according to this logic, I created an alias , I have another theory: since this is an implicit conversion, the copy constructor is called :
Type& Type::operator=(const Type&);
Type b = Type(a);
Type b = (operator=(&(Type(&a))))//Абстракция, не сочтите за ошибку

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
polar_winter, 2018-03-26
@polar_winter

#include <iostream>

class A 
{
public: 
    A()
    {
        c = counter++;
    };

    int c;   
 
private:

    static int counter;
};

int A::counter =0;

int main (int argc, char ** argv)
{
    A a1;
    A a2;
    A a3;
    std::cout << a1.c << a2.c << a3.c << std::endl;
    std::cout << (a2 = a1).c <<std::endl;
    std::cout << a1.c << a2.c << a3.c << std::endl;
    
    a2.c = 1;
    std::cout << a1.c << a2.c << a3.c << std::endl;
   
    return 0;
}

012
0
002
012
You probably misunderstood what the assignment operator means to return. Second stdcout output

M
Mercury13, 2018-03-27
@Mercury13

And this means that by manipulating the members of b I will edit the memory of a?

Of course not.
The task of the assignment operation is to make the object on the left (aka *this) become a copy of the object on the right (aka x).
Why does the assignment operator take a Type& by reference? Yes, because to take a copy is to call the copy constructor. It is quite possible to write
when the type is “large” on the left, and “small” on the right, and its copy constructor is negligible (or even equals the overhead of a reference, like that of the same int). But when the same type is on both sides, this is often a thoughtless call to the copy constructor (their own copy constructor and operation = are usually written when tricky logic is needed, and not trivial copying).
Why does the assignment operator return Type&? Just to make constructions like a = b = c work. It can also return void if you want.
class C
{
public:
    C() {}
    C (const C&) { std::cout << "Copy ctor" << std::endl; }
    C & operator = (const C&) { std::cout << "Assign" << std::endl; return *this; }
};

int main()
{
    C a;
    C b = a;

Will output "Copy ctor". That is, the answer to the first question is the copy constructor.
Not assignment, but initialization. The assignment would be...
Type b;
b = a;

Ideally, there are two copy constructors and one destructor, but the compiler is free to reduce their number. Therefore, only "copy ctor".
The code
int a = 10;
C b = C(a);

also gives one call to C(int). And C(const C&) is never called.
UPD. One of the innovations of C++11 is that even without these non-guaranteed optimizations, when one object disappears and the second one appears, it would be possible to carry out something simpler instead of copying, which does not require memory allocation-return. I experimented in C++03 mode, but C++11 would already have a copy constructor, a move constructor, and a destructor.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question