Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing object by reference in C++

The usual way to pass a variable by reference in C++(also C) is as follows:

void _someFunction(dataType *name){ // dataType e.g int,char,float etc. /**** definition */ }  int main(){     dataType v;     _somefunction(&v);  //address of variable v being passed     return 0; } 

But to my surprise, I noticed when passing an object by reference the name of object itself serves the purpose(no & symbol required) and that during declaration/definition of function no * symbol is required before the argument. The following example should make it clear:

// this #include <iostream> using namespace std;  class CDummy {   public:     int isitme (CDummy& param);     //why not (CDummy* param); };  int CDummy::isitme (CDummy& param) {   if (&param == this) return true;   else return false; }  int main () {   CDummy a;   CDummy* b = &a;   if ( b->isitme(a) )               //why not isitme(&a)     cout << "yes, &a is b";   return 0; } 

I have problem understanding why is this special treatment done with class . Even structures which are almost like a class are not used this way. Is object name treated as address as in case of arrays?

like image 476
KNU Avatar asked Aug 09 '13 12:08

KNU


People also ask

Are objects passed by reference in C?

There are references in C, it's just not an official language term like it is in C++. "reference" is a widely used programming term that long predates the C++ standardizing of it. Passing a pointer to an object is passing that object by reference.

Can you pass an object by reference?

A mutable object's value can be changed when it is passed to a method. An immutable object's value cannot be changed, even if it is passed a new value. “Passing by value” refers to passing a copy of the value. “Passing by reference” refers to passing the real reference of the variable in memory.

What is passed when an object is passed by reference?

What exactly is passed when an object is passed by reference? Explanation: The location of the object, that is, the exact memory location is passed, when the object is passed by reference.


1 Answers

What seems to be confusing you is the fact that functions that are declared to be pass-by-reference (using the &) aren't called using actual addresses, i.e. &a.

The simple answer is that declaring a function as pass-by-reference:

void foo(int& x); 

is all we need. It's then passed by reference automatically.

You now call this function like so:

int y = 5; foo(y); 

and y will be passed by reference.

You could also do it like this (but why would you? The mantra is: Use references when possible, pointers when needed) :

#include <iostream> using namespace std;  class CDummy { public:     int isitme (CDummy* param); };   int CDummy::isitme (CDummy* param) {     if (param == this) return true;     else return false; }  int main () {     CDummy a;     CDummy* b = &a;             // assigning address of a to b     if ( b->isitme(&a) )        // Called with &a (address of a) instead of a         cout << "yes, &a is b";     return 0; } 

Output:

yes, &a is b 
like image 149
keyser Avatar answered Sep 22 '22 12:09

keyser