What is difference between initialization and assignment?

Answer Posted / jaroosh

Those answers are actually highly insufficient.
Here are some differences to point that come to my mind :
a) you CANT assign to a const variable, whereas you CAN
initialize it. For example :
const AXC d = 3; //OK! initialization
d = 3; //WRONG! cannot assign to const
b) initialization IS about creating object
assignment IS about setting some value to object
This is why in the following code :
AXC d = 3;
AXC x;
x = 2;
first line will require an appropriate constructor:
AXC(int i) { ... }
whereas the third line will use overloaded assignment "="
operator if its specified (if not, it will use constructor
like above):
AXC operator =(int x) { ... }
c) Initialization also calls copy constructors, while
assignment does not :
A c;
A d = c; //Calls copy constructor of A
A e;
e = c; //Calls assignment operator of A

Is This Answer Correct ?    71 Yes 5 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain differences between new() and delete()?

609


What is public, protected, private in c++?

640


What is the history of c++?

546


Why is swift so fast?

612


Is java based off c++?

523






What is the benefit of learning c++?

541


How many ways can a variable be initialized into in C++?

588


Explain virtual class?

576


What is the need of a destructor? Explain with the help of an example.

553


What are the various arithmetic operators in c++?

567


What is the use of endl in c++ give an example?

602


When should we use container classes instead of arrays?

572


Do you know what are the new features that iso/ansi c++ has added to original c++ specifications?

552


what is the difference between overloading & overriding? give example.

555


What is near, far and huge pointers? How many bytes are occupied by them?

646