Object as pointer in method : call by value or reference | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Object as pointer in method : call by value or reference

Hi I am clear on concept of call by value and call by reference. Like same copy and local modifications are available outside of function scope for call by reference .... In case of call by value, new copy gets created and modifications inside function is lost post function scope is over Now refer code below where friend function takes object as pointer. Is this called call by value or reference or neither ? I belive it is call by pointer and it is neither call by value nor call by reference but modification is permanent after scope of function Is it necessary to put call by pointer either into call by reference or value ? https://code.sololearn.com/cI2m67BF73Bu/?ref=app

4th Apr 2021, 7:17 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
3 Answers
+ 3
I'm not sure about what the wider C++ community would call it but the pointer is copied by value so my gut feeling says that. Then again references are just pointers and so a reference, too, is copied by value. At the end of the day everything is a value and will be copied by value. "call by pointer" seems like a fine way to describe the fact that you are passing a pointer. Also fits smart pointers and what not.
5th Apr 2021, 12:07 AM
Schindlabua
Schindlabua - avatar
+ 1
Ketan, May I just ask why pass object pointer? I thought we pass object reference in C++, whereas pointer is used for (more or less) same purpose in C.
4th Apr 2021, 9:08 AM
Ipang
+ 1
Yeah that's correct Ipang ... But many times I pass pointer as argument for inherited classes. Definitely we can go with reference but I used to pass pointer of base class
4th Apr 2021, 9:29 AM
Ketan Lalcheta
Ketan Lalcheta - avatar