INTERVIEW QUESTIONS
C++
INHERITANCE IN C++
DETAILS
Question: Why Call by reference technique is used in case of Copy constructor & not call by value?
Answer: MyClass(const MyClass& obj); MyClass(Myclass obj);
In the latter one, compiler need to create a local copy of 'obj' which in turn involves the call to copy constructor,, which will create a copy of obj..which........and so on
However, in former case only one copy is created which is what we need.
Correct me if I am wrong.
|
Question:
Why Call by reference technique is used in case of Copy constructor & not call by value?
Answer:
MyClass(const MyClass& obj); MyClass(Myclass obj);
In the latter one, compiler need to create a local copy of 'obj' which in turn involves the call to copy constructor,, which will create a copy of obj..which........and so on
However, in former case only one copy is created which is what we need.
Correct me if I am wrong. Source: CoolInterview.com
Answered by: Arun | Date: 8/15/2008
| Contact Arun
consider the following statements: circle c1,c2; c1=c2; circle c3=c2; here c1,c2 are objects of the type circle which is a class. the statement c1=c2; will cause the compiler to copy the data from c2,member-by-member,into c1.this is what the assignment operator does by default. in the next statement we have initialised one object with another object during declaration.this statement causes a similar action.The compiler creates a new object c3 and copies the data from c2 member-by-member,into c3.This is what the copy constructor does by default.The difference between the two is that the copy constructor also creates a new object,where,the assignment operator doesn't. Note that in case of copy constructor we pass the arguments to functions by references.This is often desirable.Had the argument been passed by value it would have created another local object in the function.But in case of large objects this would lead to considerable wastage of memory. Source: CoolInterview.com
Answered by: AMBIKA.R.BIRADAR(AIET GULBARGA) | Date: 8/23/2008
| Contact AMBIKA.R.BIRADAR(AIET GULBARGA)
In call by value there will be local copy of the object in the calling function. what ever will be change in that copy will not affect the origional copy. but in call by reference there will be an alias of the original object will be in the calling function .so what ever we will change will affect the origional one. Source: CoolInterview.com
Answered by: nitesh | Date: 4/24/2010
| Contact nitesh
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|