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

What is a c++ vector?

584


Give 10 points of differences between C & C++.

616


What would happen on forgetting [], while deallocating an array through new?

631


What is the purpose of the "delete" operator?

622


write a porgram in c++ that reads an integer and print the biggest digit in the number

1763






What is the object serialization?

616


Why is c++ called oops?

550


Difference between a homogeneous and a heterogeneous container

657


What is the use of 'this' pointer?

761


Why use of template is better than a base class?

640


Write about the stack unwinding?

624


Is c++ a difficult language?

570


Will a recursive function without an end condition every quit, in practice a) Compiler-Specific (Some can convert to an infinite loop) b) No c) Yes

577


What is a memory leak c++?

585


What is the insertion operator and what does it do?

561