+ 7

COPY CONSTRUCTOR(sololearn misses it)

WELL IT SEEMS THAT SOLOLEARN MISSES SOME OF THE TOPICS HERE IS ONE OF THE TOPICS EXPLAINED. COPY CONSTRUCTOR A copy constructor copies value of one object to other object. Let there be a class as class abc { int a,b; public: void somefunctions(); int c,d; //NOW THE COPY CONSTRUCTOR abc(abc &ob) { a=ob.a; b=ob.b; c=ob.c; d=ob.d; } }ob1; There are three situations when a copy constructor is called. 1.abc ob2=ob1;(object defined and initialized with another object. 2.When an object is passwd by value to a function. 3.When a function returns an object. When we do not define a copy constructor the compiler creates an implicitly-defined copy constructor which is inline public member of its class. Also ob1=ob2; does not calls copy constructor. It simply assigns values of ob2 data members to ob1 data members. IMPORTANTLY copy constructor has syntax <classname>(<classname>&) This is so because if objects are passed by value then the object needs to be copied to the temporary function object. And to copy object copy constructor works itself. So it calls itself recursively until compiler complains out of memory. Well check a real code. https://code.sololearn.com/cf9IjrUkhyzy/#cpp Well if you really read it all then reply how can you stop objects prom being copied.

14th Nov 2016, 11:55 AM
Megatron
Megatron - avatar
5 Respostas
+ 2
Word const with argument of copy constructor is not a good idea?
15th Nov 2016, 5:32 PM
John Doe
+ 2
Well I tried it but in vain still same output.(without any error)
15th Nov 2016, 5:37 PM
Megatron
Megatron - avatar
+ 2
Its not in vain. Const make an argument unmodifiable.
15th Nov 2016, 5:48 PM
John Doe
+ 2
Please have a look at the modified code were you asking me to do the same. If yes it does not works. https://code.sololearn.com/cOQ6dW6H0On4/#cpp
15th Nov 2016, 5:58 PM
Megatron
Megatron - avatar
+ 1
ali.dordane@yahoo
16th Nov 2016, 6:35 AM
ali.shiraz
ali.shiraz - avatar