| | |
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
Views: 4566 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






