You are currently browsing articles tagged c++.

I recently star­ted doing soft­ware devel­op­ment on a cas­u­al basis for GPlates, at the School of Geosciences, Uni­ver­sity of Sydney. Think back to high-school sci­ence class where you learnt about Pangaea and Gond­wana­land and how the Earth’s tec­ton­ic plates have ever-so-slowly shif­ted over mil­lions of years. GPlates is soft­ware that allows sci­ent­ists to “wind the clock back” on these plate move­ments and visu­al­ise what the Earth might have looked like all these years ago. It’s open-source, so if you’re curi­ous, grab a copy and play with it.

I’m quite glad to have met the GPlates team. It’s dif­fi­cult, I think, to find qual­ity soft­ware engin­eer­ing in Aus­tralia, and GPlates devel­op­ment is led by a bunch of developers who are pas­sion­ate about writ­ing qual­ity, well-designed, best-prac­tice C++ code. It’s cer­tainly not your aver­age in-house or aca­dem­ic research soft­ware. And it sure is more intel­lec­tu­ally safis­fy­ing than work­ing in cor­por­ate IT.

Tags: ,

To kill a singleton: I found this to be a use­ful dis­cus­sion on how to write a singleton class in C++ that ensures the singleton is prop­erly des­troyed (for even mul­ti­th­readed applic­a­tions).

18 May 2009 | No comments

Here are the solu­tions to the C++ maps exer­cise I posed in this post.

The first set of prob­lems relates to the fact that the Employ­ee class has no default con­struct­or. Here’s why. In the line

	id[0] = Employee("John Smith");

what doesn’t hap­pen is that the key 0 gets asso­ci­ated with the new Employ­ee object you just cre­ated. What does hap­pen is that the id[0] part tries to default ini­tial­ise an Employ­ee object, and then assign using operator= the Employ­ee object you cre­ated on the right hand side. That’s all fine if you have a default con­struct­or, but our Employ­ee class doesn’t have one (because I’ve defined anoth­er con­struct­or but not the default con­struct­or). Without chan­ging the class defin­i­tion to add a default con­struct­or, you will need to expli­citly insert the key-value pair into the map, like this:

	id.insert(make_pair(0, Employee("John Smith")));

But what about this line?

	cout  id[0].name  endl;

Surely, it wouldn’t be try­ing to call the default con­struct­or here, because I am merely retriev­ing the value of id[0], which I know has already been con­struc­ted? But at com­pile time, how would the com­piler know wheth­er the call to id[0] will res­ult in a new object being con­struc­ted or not even if you do? To get around this prob­lem, you’ll have to go around the long way:

	cout  id.find(0)->second.name  endl;

So the mor­al of the story is write a default con­struct­or (if it makes sense to do so)! Note that operator[] is also unavail­able when you have a const map. (Why?)

For the second lot of prob­lems, the root of the prob­lem is that you are using Employ­ee as the key type of the map. You can only use a class as a key if you can order objects of that class, so you’ll have to write an operator< for Employ­ee.

Tags: ,

Why doesn’t the fol­low­ing code com­pile? Without chan­ging the defin­i­tion of the struct Employ­ee, can you make it com­pile so that it does what it’s meant to do?


using namespace std;

struct Employee
	string name;
	Employee(const string& s) : name(s) { }

int main()
	map id;
	id[0] = Employee("John Smith");
	id[1] = Employee("Mary Jane");
	cout  id[0].name  endl;
	map id2;
	Employee a("A"), b("B");
	id2[a] = 100;
	id2[b] = 200;
	return 0;

Answers here.

(I gave this exer­cise to my C++ stu­dents a couple of weeks ago.)

Tags: , ,