Question regarding code in tutorial

On page 98 of the pdf version of the tutorial the following code is shown:

CVector& CVector::operator= (const CVector& param)
{
x=param.x;
y=param.y;
return *this;
}

For some reason I don't remember the tutorial ever discussing what a return type a& is. The tutorial certainly has discussed that return type a* would be a pointer to an object of class/type a. Therefore does a& mean a the value pointed to by a pointer? In fact the function is returning the dereferenced "this" which would seem to be consistent with what I think a& means. And if this is the case, I don't think I understand what is actually being returned if the pointer is pointing to an object of a class. In this example the class has the form

class CVector {
public:
int x,y;
CVector () {};
CVector (int,int);
CVector operator + (CVector);
}

and so how can this entire class be passed as a value? Thanks for your help.
CVector& is a reference to class CVector this is a pointing to the class object whose + operator was calling
so *this is that object itself
Returning CVector would create a copy of it, returning CVector& would return the exact object

The tutorial talks about references on the section Functions II but it describes their use as parameters
http://www.cplusplus.com/doc/tutorial/functions2/
Last edited on
a& is neither a pointer nor a copy of an object of type a, but exactly an object of a itself.

http://www.parashift.com/c++-faq-lite/references.html#faq-8.1

 
CVector operator + (CVector);

You are passing by value a CVector object (not the entire class).
Usually we prefer CVector operator + (const CVector&); so the compiler can just use the object itself.
OK thanks for all that. I guess there is a lot more here than meets the eye; would be nice if the tutorial discussed all this instead of leaving the individual confused. Anyway, let me see if I have it nailed.

Let us say sometype is some type. To declare X as being of type sometype we say

sometype X;

To declare X as a pointer to an object of sometype we say

sometype* X;

If X is an argument to a function and we wish to declare it as a pointer to an object of sometype then we declare it as

void function name (sometype& X);

If we wish to declare that a function returns a pointer to an object of type sometype then we say

sometype* function name (arguments);

If we wish our function to return an object of type sometype, but we wish it to return the original object that came into existence during the running of the function, and hence not have it disappear as would normally be the case of a local object, then we have to declare it as

sometype& function name (arguments);

If we wish our function to return an object of type sometype, but it is ok for it to return a copy of the object our function was working with then we can say

sometype function name (arguments);

Do I have this correct? And if so then what meaning would specifying an argument in a function as sometype* X have as in

void function name (sometype* X);
Last edited on
No, you cannot return local objects.

However, you can return an extant object.

Here are two examples.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
using namespace std;

struct point
  {
  static int current_id;
  int id;
  int x, y;

  point( int x = 0.0, int y = 0.0 ):
    id( current_id++ ),
    x( x ),
    y( y )
    { }

  point& operator += ( const point& pt )
    {
    x += pt.x;
    y += pt.y;
    return *this;  // return the previously existing *this object
    }

  point operator + ( const point& pt )
    {
    point result( x, y );
    result += pt;
    return result;
    }
  };

int point::current_id = 0;

ostream& operator << ( ostream& outs, const point& pt )
  {
  outs << "point #" << pt.id << ":(" << pt.x << ", " << pt.y << ")";
  return outs;  // return the previously existing ostream object
  }

int main()
  {
  point v1( 5, 7 );
  point v2( 3, -4 );

  cout << "v1:       " << v1 << endl;
  cout << "v2:       " << v2 << endl;

  cout << "v1 + v2:  " << (v1 + v2) << endl;

  cout << "v1 += v2: " << (v1 += v2) << endl;

  return 0;
  }

Notice how (v1 + v2) produces a new point (point #2), where (v1 += v2) does not -- it returns the original point #0.

Also, this example shows you how to properly overload the + and += operators. The + operator could have been shortened a bit:
1
2
3
4
  point operator + ( const point& pt )
    {
    return point( x, y ) += pt;
    }

Hope this helps.
Topic archived. No new replies allowed.