The assignment operator in C++

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 ?
when i initialize a variable am I doing it with the assignment operator ?
No.

When i assign to a variable, am I doing it with the assignment operator ?
Yes.

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 ?
Yes.
@helios why this is true : 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 ?

if this is not : when i initialize a variable am I doing it with the assignment operator ?
Why do they seem contradictory to you?
@helios becauase first you say that if i initialize a variable I'm not doing that with the assignment operatot
when i initialize a variable am I doing it with the assignment operator ?
No.


then I say that when we initialize a variable we're doing it with the assignment operator and you say that I'm right
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,


Sorry I'm just a beginner
Sorry, I missed this bit:
we are giving it a new value with the assignmnet operator


Initialization of the form type object = value; uses the copy constructor.
Perhaps code will be illustrative?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

struct A
{
    A(int) {std::cout << "initialization\n";}
    A(const A&) { std::cout << "copy initialization\n"; }

    A& operator=(const A&) { std::cout << "copy assignment\n"; return *this; }
};

int main()
{
    A a(34);    // initialization
    A b(a);     // initialization
    A c = 69;   // initialization
    A d = c;    // initialization

    a = d;      // assignment
}
initialization
copy initialization
initialization
copy initialization
copy assignment
Topic archived. No new replies allowed.