The Constant Coder

February 12, 2007 on 9:49 pm | In programming |

It has been too long since I had an entry about programming. I mean a real entry about programming, not just some random quip about Java or the wonder of Python. Today let’s talk about C++ a little.

C++ was designed a few decades ago and for all its potential power- it is a double edged sword. I am not the first person to say this, but it is so undeniably true that I don’t feel too bad about repeating it. C gave you plenty of rope to hang yourself and C++ sort of continues that thread, but goes a little further- providing rope in a variety of lengths, colors and even ropes that have a noose already tied in them.

That being said, software can be written in C++- good software, and it doesn’t have to be insanely painful. Simple guidelines can really help this. Basic things like, prefer references to pointers. Why prefer using a reference?

1) Using references tends to encourage allocating memory on the stack instead of the heap, it is hard to leak something you never allocated, and 2) a reference cannot be re-assigned. Yup no more memory leaks from careless pointer manipulation.

What is a reference anyway? A reference literally is the variable it refers to, also known as the “referent”. That is why you cannot re-assign it- the operation doesn’t have a meaning. Consider a simple reference and its pointer equivalent:

string& my_str = func_returns_a_ref();

is the same as:

string* const my_str = func_returns_a_ptr();

Don’t confuse that with const string* my_str, they are very different. The former is a constant pointer, whereas the later is a pointer to a constant. If you poke around with references you will find that you can use them pretty much anywhere you would have used a pointer. Though somethings do look a little unnatural if you try and force the use of references. For example:

string* my_string_function() {
// do something and return a string pointer
}
...
string& mystr = *(my_string_function());

That looks weird to me too. So maybe forcing the reference thing isn’t such a good idea, maybe we should just use them when they make sense. Since you already limited in the operations you can use on a reference, it is pretty common to take the next logical step and use references for const variables, instead of pointers. This quite useful for accessor methods that return handles to member variables. Instead of saying:

class MyClass {
public:
const string* my_var() { return &_var};
private:
string _var;
}

use this form:

class MyClass {
public:
const string& my_var() { return _var};
private:
string _var;
}

Great! The code is cleaner (we lost that ampersand on _var) and we have really locked down the handle we returned to _var. Not only is the variable const, but the handle to the variable is immutable as well. Sweet! we protect our data, and stopped the caller from potentially doing something stupid as well! Double word score!

For more info on this kind of stuff, check out the C++ FAQ Lite. It has more examples and most importantly- good explainations of C++ constructs. Enjoy!

1 Comment »

RSS feed for comments on this post. TrackBack URI

  1. You are my hero!

    Comment by Hadley Beeman — February 14, 2007 #

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

Powered by WordPress
^Top^