You are currently browsing articles tagged c++.

I recently started doing software development on a casual basis for GPlates, at the School of Geosciences, University of Sydney. Think back to high-school science class where you learnt about Pangaea and Gondwanaland and how the Earth’s tectonic plates have ever-so-slowly shifted over millions of years. GPlates is software that allows scientists to “wind the clock back” on these plate movements and visualise what the Earth might have looked like all these years ago. It’s open-source, so if you’re curious, grab a copy and play with it.

I’m quite glad to have met the GPlates team. It’s difficult, I think, to find quality software engineering in Australia, and GPlates development is led by a bunch of developers who are passionate about writing quality, well-designed, best-practice C++ code. It’s certainly not your average in-house or academic research software. And it sure is more intellectually safisfying than working in corporate IT.

Tags: ,

To kill a singleton: I found this to be a useful discussion on how to write a singleton class in C++ that ensures the singleton is properly destroyed (for even multithreaded applications).

18 May 2009 | No comments

Here are the solutions to the C++ maps exercise I posed in this post.

The first set of problems relates to the fact that the Employee class has no default constructor. Here’s why. In the line

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

what doesn’t happen is that the key 0 gets associated with the new Employee object you just created. What does happen is that the id[0] part tries to default initialise an Employee object, and then assign using operator= the Employee object you created on the right hand side. That’s all fine if you have a default constructor, but our Employee class doesn’t have one (because I’ve defined another constructor but not the default constructor). Without changing the class definition to add a default constructor, you will need to explicitly 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 trying to call the default constructor here, because I am merely retrieving the value of id[0], which I know has already been constructed? But at compile time, how would the compiler know whether the call to id[0] will result in a new object being constructed or not even if you do? To get around this problem, you'll have to go around the long way:

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

So the moral of the story is write a default constructor (if it makes sense to do so)! Note that operator[] is also unavailable when you have a const map. (Why?)

For the second lot of problems, the root of the problem is that you are using Employee 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 Employee.

Tags: ,

Why doesn’t the following code compile? Without changing the definition of the struct Employee, can you make it compile 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 exercise to my C++ students a couple of weeks ago.)

Tags: , ,