Question

Could someone please explain the difference to me in very small words? I have never understood this and am very confused in my current project. What I am trying to do is fix this code:

const Multinumber& Pairs::operator+(const Multinumber &rhs) const
{
    const Pairs &_rhs = dynamic_cast<const Pairs &>(rhs);
    Pairs toreturn(_rhs.X_value+X_value,_rhs.Y_value+Y_value);
    return toreturn; //reference to local variable? fix this?
}

Now my compiler tells me that this is a reference to a local variable, but it won't let me turn toreturn into a pointer, because they are somehow different from references. And then, I am also working on a set class, which is supposed to hold references or pointers to objects of an abstract class. I am totally confused. Any help would be much appreciated.

Was it helpful?

Solution

You do seem very confused :) Ok, so:

Essentially, a pointer is just a variable that stores the address of another variable. For instance, suppose I have an int called i, I can store its address in a pointer p:

int i = 23;
int *p = &i;    // p has type int* (pointer to int) and stores &i (the address of i)

If I then want to change the thing it points to (i.e. the variable whose address it stores), I just assign to *p -- this is the syntax used to denote the thing pointed to. In this case, *p refers to i. Thus:

*p = 9;     // sets i to 9 (since *p is i)

I can reseat the pointer (i.e. make it point to something else) just by assigning to p, i.e.

int j = 84;
p = &j;     // store j's address in p, overwriting what was there before (i.e. i's address)
*p = 18;        // sets j to 18 (since *p is now j)

Now, a reference is slightly different. A reference creates an alias for a variable:

int i = 23;
int& r = i;     // r has type int& (reference to int) and refers to i

Note that references may be implemented in terms of pointers (or they may not, particularly when the compiler starts optimizing things), but that's irrelevant from the programming perspective -- all that matters to us here is the way the language works.

If you want to change the thing referred to (i.e. i in this case), you just do:

r = 9;      // sets i to 9 (since r is an alias for i)

Unlike pointers, references cannot be reseated. As just shown, assigning to r changes the thing you're referring to (i), not the reference itself. Furthermore, because references cannot be reseated, they must be initialized immediately. This is not the case with pointers. In other words:

int *p;     // legal
int& r;     // bad

One final basic difference is that pointers can be NULL, indicating that they are not pointing to anything. This just means they contain the address 0. References must always refer to an actual object. This difference can be important to implementers, because they can then use pointers to implement a Maybe type, i.e. if the pointer is not NULL, then make use of the pointed-to object, otherwise do something else. They can't do the same thing with references.

Hope that's clear as regards pointers vs. references!


Now, regarding your operator+ -- the purpose of an addition operator is to add two objects and return a new object representing their sum. So if I had a 2D vector type, I might write an operator+ for it as follows:

Vec2 operator+(const Vec2& lhs, const Vec2& rhs)
{
    return Vec2(lhs.x+rhs.x, lhs.y+rhs.y);
}

In your code, you are trying to return a local object toreturn by reference -- this doesn't work, because toreturn ceases to exist at the end of the operator. Instead, you should return by value here. Incidentally, you would encounter the same problem if you tried to return a pointer, e.g.

Vec2* operator+(const Vec2& lhs, const Vec2& rhs)
{
    Vec2 result(lhs.x+rhs.x, lhs.y+rhs.y);
    return &result; // bad!
}

In that code, result ceases to exist at the end of the operator, so the pointer you return would end up pointing to an invalid location. Bottom line -- don't try anything fancy, return by value in this sort of situation.

OTHER TIPS

First, your signature is wrong. It should be:

Multinumber Pairs::operator+(const Multinumber &rhs) const;

operator+ should return a new object, not a reference to either argument.

As for the difference between references and pointers, this detailed question right here on SO. It covers all the basics, and the some.

Pointer has some address where the object is. And the reference is as the alias for the pointer and it means that you don't have to dereference it. But the usage is similar - don't copy objects, only work with the origin.

You have the variable toreturn as a local variable which means that the compiler generates the destructor for this object at the end of method. So you are trying to return destroyed object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top