943,699 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7744
  • C++ RSS
Mar 1st, 2006
0

Exact meaning of functions returning reference

Expand Post »
How are functions returning reference used?? Do they return values on the left side or the right side of the assignment. I would be glad if I could get some help.
Thanks,
comwizz.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
comwizz is offline Offline
39 posts
since Nov 2005
Mar 1st, 2006
0

Re: Exact meaning of functions returning reference

Quote originally posted by comwizz ...
How are functions returning reference used?? Do they return values on the left side or the right side of the assignment. I would be glad if I could get some help.
Thanks,
comwizz.
Try looking at this and post further queries
http://www.parashift.com/c++-faq-lit...s.html#faq-8.2
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Mar 2nd, 2006
0

Re: Exact meaning of functions returning reference

Why should references be used at all?? As we could always use pointers in place of them as if we change the value of the pointer , automatically the value of variable it points to is changed eg. p=&s;*p=7;changes the value of s also. Also , would a function returning a reference be used on the right side of assignment operator ?? eg. c=max();where max returns a reference variable having similar datatype to c.Please reply.
Thanks,
comwizz.
Reputation Points: 10
Solved Threads: 0
Light Poster
comwizz is offline Offline
39 posts
since Nov 2005
Mar 2nd, 2006
0

Re: Exact meaning of functions returning reference

one place a reference is needed and pointers will not work is overloading the [] operator. You couldn't implement this as neatly using pointers.

And the reference can be used on both left and right side of the operators, as illustrated in main() below.

#include <iostream>
using namespace std;

class matrix
{
private:
	int array[10];
public:
	matrix() {memset(array,0,sizeof(array));}
	int& operator[](int index) {return array[index];} 

};

int main()
{

	matrix m;
	m[0] = 1;
	m[1] = 2;
             int x = m[2];
	cout << m[0] << endl;
	cout << m[1] << endl;

	return 0;
}
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Mar 2nd, 2006
0

Re: Exact meaning of functions returning reference

Quote originally posted by comwizz ...
Why should references be used at all?? As we could always use pointers in place of them as if we change the value of the pointer , automatically the value of variable it points to is changed eg. p=&s;*p=7;
I think you almost answered your own question - why use references? Well, because pointer syntax can look ugly. pointers are also error prone. In general, use References whereever you can, only use Pointers when you have to.
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: ASCII art help
Next Thread in C++ Forum Timeline: please help me with array in a class





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC