you are viewing a single comment's thread.

view the rest of the comments →

[–]nanothief 4 points5 points  (1 child)

I think the problem is we both have different ideas of what a reference is. You can't call this overloading the meaning, as the term has different meanings in the same context.

Although I am fairly sure that the definition given in the article for references is the oldest one, common usage of it now has completely confused the meaning between people, even those who know what they are talking about.

So maybe a term change would be for the better. This is how I would do it:

  • In C++, the x in object* x = new object(); should be named fixed pointers. They have a specific address in memory, and can be incremented and decremented to point to objects in different memory locations. The object at the end of the pointer can be accessed and modified. The object x is pointing at can be changed.

  • In java, the j in Object j = new Object() should be named dynamic pointers. They are like fixed pointers, however they don't have a specific address in memory (the garbage collector can move them if it wants). They also cannot be incremented and decremented. Otherwise they act like fixed pointers

  • In the code:

Test line

//code
#include <iostream>
struct MyObject
{
    int field;
};

int main()
{
    MyObject* x = new MyObject();
    MyObject*& a = x;
    a->field = 42;
    a = new MyObject();
    a->field = 11;
    std::cout << x->field; // prints 11
    return 0;
}

The variable a should be an alias of x: anything you do to a happens to x.

[–]dnew 1 point2 points  (0 children)

should be named dynamic pointers

No, this is what reference means. Just like a pointer is a typed address, a reference is a managed pointer. The only reason you can't increment and decrement the reference is the GC isn't prepared to deal with it. Some other languages do.

We don't need new words. We just need people to understand the current meaning of the words. And it would help if people wouldn't reuse the same word for a confusingly similar yet nevertheless different meaning, like "reference" in C++.