Jump to content
  • entries
    2
  • comments
    0
  • views
    803

Const, Pointers, References, and Right To Left Reading.

straight_stewie

1,226 views

This will be a very short blog post, but I hope that it helps someone.

I've noticed that many people learning C++ struggle with understanding pointers, references, variables, and the const keyword. So I plan to write a few charts and some words that can be used as a sort of reference to help. I will not try to go above and beyond in explaining what pointers, references, and const are, as there are already many resources that do wonderful jobs of that.

 

General Rules

There is really only one rule I want to touch on here. There are generally two styles of declaring pointers and references in C++

  • Put the asterisk by the type, or put the asterisk by the variable name.
    • int* x;
    • int *x;

Many people prefer the second one. They claim that when you are declaring multiple variables in one line, it prevents you from accidentally declaring a pointer to a variable of type, and a variable of a type.

int* x, y;  // x is a pointer to an int, and y is an int
int *x, *y; // x and y are pointers to ints.

Of course, consistency is key when it comes to readability. So the same rule should apply in all situations. There is generally no good way to make easily readable complex parameter declarations if you adopt the second style shown above. But clearly, you should adopt the second style shown above to prevent errors. Well, I believe that right to left readability is of utmost importance to writing good C++, and so the rule that I use to make everything consistent is: Don't declare multiple variables in one statement.

int x, y; // bad.

// good
int x;
int y;

But what is right to left readability and why do I believe it is so important to write code in such a way that it is maintained?

 

Right to Left Readability

Right to left readability is a simple principle, and it helps one understand pointers, references, and const statements so greatly that I believe developers should make it a habit to write code that matches this principle. Ostensibly, the rule says that I should be able to read a parameter declaration or the left side of an assignment from right to left and end up with a valid English sentence that says what the declaration or assignment is doing. Some examples:

  • int a;

    • a is an int

  • int* a;

    • a is a pointer to an int

  • int &a;

    • a is a reference to an int

Notice the algorithm I'm using to read the declarations here: First, read the name of the variable. Then, insert a verb, in this case, some form of "is a". Then state whether it is a pointer, a reference, or nothing, and then state the type.

 

One level of const

  • const int a;
    • a is an int that is const
  • const int* a;
    • a is a pointer to an int that is const
  • const int &a;
    • a is a reference to an int that is const

Notice that we've added a new form of is: "that is". In this case we use "that is" to refer to the int, and not the reference or the pointer. For example, the pointer can be changed, but the integer that the pointer references cannot.

 

One level of const, redux

  • int const a;
    • a is a const int.
  • int* const a;
    • a is a const pointer to an int
  • int& const a;
    • a is a const reference to an int. (notice that this is nonsense, all references are const in this way; meaning that the reference cannot be changed once assigned.)

Here, we've introduced yet another verb: to. In this case, const means that the variable, whether it be a pointer, a reference, or an object cannot be changed. So, int* const a is a pointer to an int and that pointer cannot be changed. Also notice that const int a and int const a are identical.

 

Two levels of const

  • const int* const a;
    • a is a const pointer to an int that is const
      • This means that a is a pointer that cannot be changed and that it points to an integer that cannot be changed.
  • const int& const a;
    • a is a const reference to an int that is const
      • a is a const reference to an int that is const
      • Please notice that this is nonsense. All references are const in the sense that the reference cannot be changed. However, in this case, the object that a refers to also cannot be changed.

And that's all there is to it. It's really not that complicated, once you learn the appropriate way to write it out and read it right to left. Just incase my hurried formatting above wasn't easy enough to read, here is a chart below. I will leave out invalid entries, such as const references.

 

Full Chart

  • int a;
    • a is an int.
  • int* a;
    • a is a pointer to an int
  • int& a;
    • a is a reference to an int
  • int const a;
    • a is a const int.
      • a cannot be changed
  • int* const a;
    • a is a const pointer to an int
      • the pointer to a cannot be changed
  • const int a;
    • a is an int that is const
      • a cannot be changed
  • const int* a;
    • a is a pointer to an int that is const
      • the pointer can be changed, but the int pointed to cannot be changed through a.
  • const int& a;
    • a is a reference to an int that is const.
      • the int that is referenced cannot be changed
  • const int* const a;
    • a is a const pointer to an int that is const.
      • the pointer cannot be changed, and neither can the int that is pointed to.

0 Comments

There are no comments to display.

×