Const in C++, a brief overview

A word of warning at the beginning: This post is about C++ and not about C! So whatever you read here may not necessarily apply to C. Nevertheless you may have a look Sugih’s page which features are in C. Besides I’m not going to explain what references and pointers are.

Introduction

While working on the KIARA project writing C++11 code I was faced with the task of passing variables to functions which will not alter these. Or pure getter methods. And after reading a post about C++ constness I was left with more questions than really understanding. A quick search revealed thorough FAQ about const usage in C++. Here I’d like to make a short round-up of it.

How to read const

First of all, read it from right-to-left. For instance int const* const p would be read as “p is a constant pointer to a constant int”.

A few examples

string const& s

Let’s say you read something like this:

void MyClass::func (std::string const& s);

So reading from right-to-left this would say “s is reference to a constant std::string”. This implies that func is not going to modify s. But keep in mind that you may have a dangling reference here, especially if you work with multi-threaded code. If this would be a pointer instead of a reference it is still possible the pointer to be NULL.

string* const s

void MyClass::func (std::string* const s);

Again, read from right-to-left: “s is a constant pointer to a string”. s may be modified (the object itself) but s may not point to a different object. As you may have already noticed it’s not that hard to read when you stick to the “read from right-to-left” rule.

string const* const s

void MyClass::func (std::string const* const s);

This would read “s is a constant pointer to a constant string” and therefore the function guarantees (respectively the compiler enforces it) that the content of s may not be changed nor may s point to a different object.

Similar consts

Obviously some signatures mean the same thing:

void MyClass::func (const std::string& s); // you may know this from C
void MyClass::func (std::string const& s);

void MyClass::func (const std::string* s);
void MyClass::func (std::string const* s); // equivalent
void MyClass::func (std::string* const s); // Pitfall: NOT equivalent

Now, it doesn’t matter which version you use but is more a decision you have to make. If previously written code opts for one variant you should for the sake of consistency use the same way.

Methods that do not change its object

When you write a pure getter method you may want to tell your compiler that the following code may not change the object itself.

std::string const& MyClass::func () const;

The last const indicates that this function may not alter its objects data.

Conclusion

C++ is not a language you master in a few hours of training, C++ is like meditating and takes daily training.

The FP7 GEYSER Kick off meeting

Seán attended the kick off meeting for the GEYSER project in Rome from Nov 28-29 on behalf of ZHAW.

GEYSER is an exciting new project focused on improving energy efficiency across networks of Data Centres. It’s a 3 year project with a strong group of partners. Led by Engineering, the project team includes ABB, Wattics, Singular Logic, Green IT Amsterdam, SARARWTH/E.on Energy Research CentreTechnical University of Cluj-Napoca and ZHAW.

Continue reading

Seán Murphy

Seán has a diverse background spanning the academic space, large industry and has dabbled in the world of startups. Having completed his PhD in telecommunications in DCU in 2001, Seán then went on to work in a nascent research lab in Ericsson in Ireland. He left this lab to join the Performance Engineering Lab in UCD and spent 10 years on the leafy Belfield campus. Following a stint in Poland, working on a Marie Curie project, he joined ICCLab with a mandate to drive work in Energy Efficiency and Cloud Computing.

sean-joins-icclab

Seán on his first day in ICCLab

Seán has experience of many aspects of the crossover of telecommunications, networking and cloud computing. Working on projects ranging from different flavours of wireless access to video distribution mechanisms to mobile applications to automotive IT, he has a broad experience which he can bring to bear in the lab. Seán has a keen interest in commercial opportunities and is excited to explore the potential offered by the ICCLab environment in this regard.

Seán will lead ICCLab activities in Green Cloud Computing. Initially, this will be done through the vehicle of the FP7 GEYSER project, but is working on more initiatives to broaden the scope of these activities.