Exact meaning of functions returning reference

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2005
Posts: 39
Reputation: comwizz is an unknown quantity at this point 
Solved Threads: 0
comwizz's Avatar
comwizz comwizz is offline Offline
Light Poster

Exact meaning of functions returning reference

 
0
  #1
Mar 1st, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Exact meaning of functions returning reference

 
0
  #2
Mar 1st, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 39
Reputation: comwizz is an unknown quantity at this point 
Solved Threads: 0
comwizz's Avatar
comwizz comwizz is offline Offline
Light Poster

Re: Exact meaning of functions returning reference

 
0
  #3
Mar 2nd, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,363
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Exact meaning of functions returning reference

 
0
  #4
Mar 2nd, 2006
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;
}
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 486
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 48
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Exact meaning of functions returning reference

 
0
  #5
Mar 2nd, 2006
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC