Annoying restrictions in C++

I’m learning C++ this week. As seems always to be the case when I learn a new programming language (be it TeX, TikZ, or this), my first test project uses ridiculously advanced techniques and generates a lot of questions I can’t find any answers to in my book or even online. In this case, the question concerns “references”. (For the sake of my family, most of whom don’t know C++, I am writing this very gently. The precise version of my question is on Stack Overflow.)

Let me “recall” what a reference is in C++: it’s a type of variable that serves as the name for another variable. You can then pass the reference around rather than copying the variable itself, which has two benefits:

  1. It saves on copying time on those occasions that you really don’t need an independent clone of the object in question;
  2. It liberates the compiler from having to decide what, exactly, the referenced object consists of.

The first one is cute but not really the point; you can do that with “pointers” as well.  The second one is at the heart of the object-oriented language features, because it allows you to write code “depending on” only some general features, passing around only references to things, and then bind those references later to objects of a more specific type (which implements the general features and then some). You don’t have to know what that type is when writing the general code.

There’s just one annoying problem: references are permanent. I can’t define a reference variable, bind it to one object, and then re-bind it to a different object later. Each reference is truly a unique name. Normally this is not really an issue: if I want to reference several objects, I define several reference variables.

But that’s not possible if I want to keep a reference in a class, because a class must have a predetermined set of member objects, among them the reference variables I will use. (Let me “recall” that a class is basically a particular arrangement of various things, called members.) So if I want to successively store references to various things in a class member, I’m out of luck. I can only store one.

Okay, well, that can be gotten around by using a pointer instead of a reference. Let me “recall” that a pointer is essentially a numeric variable containing the location in the computer’s memory of some other object. As such, it is pretty low-level and is less a “name for an object” than a “place to put an object”. Pointers can be reassigned; you can just change the place they mention. But pointers are different from the object they refer to, whereas references are the same as their referents whenever the value is actually looked up. This means that there’s an extra step in obtaining the pointed-to object: a “dereference”.

The problem with this is that when I dereference a pointer, I force the compiler to acknowledge what it points to. Effectively, if I write the dereferencing in the code, the compiler thinks I actually want a copy of whatever is pointed to; if the pointer says it points to something very general, that’s what I get, even if that general thing is part of something more specific. I hear you telling me not to dereference it if I don’t like that. Well, I have to: I am trying to write code that wants values, not pointers. A reference refers to a value, so it would be usable; a dereferenced pointer is a value, but a predetermined one, and so is usable but not sufficiently generic.

So, why is it that I can’t reassign references?

This entry was posted in C++, Computers, Programming. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s