Main menu

Monthly Archives: December 2013

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.

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.

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.