| | |
Exact meaning of functions returning reference
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
•
•
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.
http://www.parashift.com/c++-faq-lit...s.html#faq-8.2
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.
Thanks,
comwizz.
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.
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;
}•
•
•
•
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;
![]() |
Similar Threads
- fstream Tutorial (C++)
- learning php (PHP)
- returning by reference (C)
- Dictionary of Functions (Python)
- Classes (C++)
Other Threads in the C++ Forum
- Previous Thread: ASCII art help
- Next Thread: please help me with array in a class
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






