C Books Guide and List
C++ Books Guide and List
Best Java Books

3,749 questions

4,049 answers

114 comments

10,705 users

What happens if you return a reference in c++?

0 votes
41 views
asked by anonymous

1 Answer

0 votes
 
Best answer
The function call can appear on the left hand side of an assignment operator.

This ability may seem strange at first. For example, no one thinks the expression f() = 7 makes sense. Yet, if a is an object of class Array, most people think that a[i] = 7 makes sense even though a[i] is really just a function call in disguise (it calls Array::operator[](int), which is the subscript operator for class Array).

 class Array {
 public:
   int size() const;
   float& operator[] (int index);
   ...
 };
 
 int main()
 {
   Array a;
   for (int i = 0; i < a.size(); ++i)
     a[i] = 7;    // This line invokes Array::operator[](int)
   ...
 }
answered by daniel Advisor (5,600 points)

Related questions

0 votes
1 answer 54 views
54 views asked by anonymous
+1 vote
1 answer 3,775 views
3,775 views asked by sandy Newbie (130 points)
0 votes
2 answers 40 views
0 votes
1 answer 73 views
0 votes
1 answer 91 views
91 views asked by anonymous
...