passing arguments by reference.

Pages: 12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream>
using namespace std;

class something {

      
      int m_space;
      
      public:
             
             void value(int space) { m_space = space;}
             int& getValue() {return m_space;} // reference declared here.
};

int main() {

something nothing;

nothing.value(5);

cout << nothing.getValue() << endl;




cin.get();
cin.get();
return 0;
}


this is a fully functioning program. I just want to ask,,,

int& getValue() {return m_space;}

and

int getValue() {return m_space;}

they do the same thing. Is there a point adding a reference at all? if so, why do programmers often add references to a function?
If you returned a reference, you could do the following:
nothing.getValue()=5;

Clearly, this is not desirable in this case.

if so, why do programmers often add references to a function?

You cannot "add references to a function". Be more specific.
They don't do the same thing. Simple as that
let's just put it really simply-

when would you use references to function in classes?
You're really returning a reference, not passing it.

Anyway, it might cout the same thing, but it isn't the same thing. Just as a pass-by-reference can change the original's value, so can a return by reference:

Another way to say it, perhaps better, the first returns an int, the second returns m_space.

1
2
3
4
5
6
7
8
9
10
11
12
int main() {

	something nothing;
	nothing.value(5);

	int *i = &nothing.getValue();
	*i = 8;

	cout << nothing.getValue() << endl;

	return 0;
}


Really messes with your private variable, huh? :)

You would first want const int& getValue();

Returning a reference to a primitive data type isn't very useful. However, if you want to return an object, then you would want/need to return the reference as to not copy the contents of your object on the stack.
Last edited on
Or write a copy constructor :) But returning a reference is probably easier
can you give me an example of returning an object using reference?
1
2
3
4
5
myClass& get()
{
   static myClass myObject;
   return myObject;
}


EDIT:
Typo. And adding static to make Framework happy.
Last edited on
closed account (zb0S216C)
@ResidentBiscuit: You better make myObject static :) You don't want undefined behaviour, now, do you?

Wazzak
You don't want undefined behaviour, now, do you?


It keeps things exciting :)
type name (myclass& NameObject) { /*blah*/ }

^ what about that? is that a good way of using reference?

EDIT:I put the ampersand in the wrong spot. Damn.
Last edited on
is that a good way of using reference


Close, but no cigar.

type name (myclass& NameObject) { /*blah*/ }

Ampersand in the wrong spot.
I did that on purpose. :)
so,correct me if I'm wrong-

-reference should be used when you need to return an object because instead of copying the object, it directly changes it. Am I right?
Sending a reference does not change the object. But you can change the object using the reference. Usually, you want to pass an object around and not actually copy it everywhere. There are situations where making a copy is necessary, but it seems to be a little less common where you need to.

I personally like to pass around pointers to my objects, instead of references, but I'd say that's just personal preference.

And if you don't pass by reference/pointer, you'll most likely need to write a copy constructor.
I just posted this in another thread but meh, it's a cool function and i like it :)

this is with vectors

 
string& name (vector<string>& list, int i); // function prototype 

1
2
3
4
5
6
7
8
9
10
11
   vector<string> list; // vector called list
   list.push_back("bob");
   list.push_back("monique");
   list.push_back("victoria");
   list.push_back("billy"); // populated..

   for ( unsigned int i = 0; i < list.size(); i++ ){ 
      cout<<name(list, i)<<endl; // displays the vector
   }

   string& chName = name(list, 1); // now chName is assigned to list[1]; 

1
2
3
string& name (vector<string>& vec, int i){
   return vec[i]; // this is the function definition
}
Honestly, that function seems a little pointless. It just wraps the operator[] with a different name for vectors of strings.
It makes my life easier in the long run

edit: i should also have said, he asked the point, this is all i can think of cause i don't really know much about references, it makes vectors easer for me because im still learning. :)
Last edited on
But you can just write;
1
2
3
for ( unsigned int i = 0; i < list.size(); i++ ){ 
      cout<<list[i]<<endl; // displays the vector
}


Interesting discussion, don't have much to add.

Here's a way to access the memory address of a char array index:
#define address(x) static_cast<const void*>x

Here's a little ditty:
1
2
3
4
5
char a[] = "A";
char *b = "B";

(&a ==  address(a))  // this is true
(&b == address(b)) // this is false 


So? A reference to a char array is the mem address of the first index. A reference to a char * is not. The value in the char* is the address of the char array.
Last edited on
is there an exception where the object is not passed by the reference?
Pages: 12