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 May 28, 2011 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 May 28, 2011 by daniel Advisor (5,600 points)

Related questions

0 votes
1 answer 54 views
54 views asked May 28, 2011 by anonymous
+1 vote
1 answer 3,774 views
3,774 views asked May 4, 2010 by sandy Newbie (130 points)
0 votes
2 answers 39 views
39 views asked Nov 22, 2010 by anonymous
0 votes
1 answer 72 views
0 votes
1 answer 89 views
89 views asked Jun 9, 2010 by anonymous
...