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
In what situations do you have to use initialization list rather than assignment in constructors?
Tell me difference between constant pointer and pointer to a constant.
Write about the scope resolution operator?
What are the differences between the function prototype and the function defi-nition?
What are single and multiple inheritances in c++?
Can you be able to identify between straight- through and cross- over cable wiring? And in what case do you use straight- through and cross-over?
Is rust better than c++?
What is the difference between an array and a list?
What is the syntax for a for loop?
What do the header files usually contains?
What are namespaces in c++?
What is a terminating character in c++?
Explain abstraction.
Why do we use iterators?
What is the word you will use when defining a function in base class to allow this function to be a polimorphic function?