Category Archives: Programming

C++ Internal Classes

C++, like Java, allows you to have internal classes. You can implement them by simply including the class in the .cpp file and not in the header file. Yep, its that simple. If you try this you will probably realise that you cant include the class as a data member. This is because when you try to include it in the class the internal class hasn’t been declared yet. That’s easy to fix, you can prototype a class by simply adding class ClassName;. Why am I telling you this? Because its an interesting design pattern, and that inner class is called a “Cheshire Class”.

The Cheshire Class is a hidden internal class. This doesn’t immediately sound useful, until you think about how C++ handles private variables. When you add a private variable to a class, even though it is not visible to the calling application, it still affects the structure of the class and hence all the code that links against it will need to be recompiled. The Cheshire class prevents this by keeping all your private variables in a private internal class. See this Example:

outerclass.h

#ifndef OUTERCLASS_H_
#define OUTERCLASS_H_

class InnerClass;

class OuterClass {
private:
  InnerClass *internal;
public:
  OuterClass();
  virtual ~OuterClass();
};

#endif /* OUTERCLASS_H_ */

outerclass.cpp

class InnerClass {
public:
  int privateVariable;
};
OuterClass::OuterClass() {
  internal.privateVariable = 0;
}

OuterClass::~OuterClass() {

}

Now instead of adding new private data to the outer class, you add it to the inner class. This way your header stays the exact same, the data structure never changes.

Note that this does take up more space, and takes a little longer to resolve because it has to jump via and extra pointer but sometimes it is worth it for that nicer solution.
Random Thought: Which is more valuable, bread or gold? Why? Which is more useful?

Pet Projects

One thing I’ve observed of people around me who are extremely passionate about computers is that they all have pet projects. For some its their work on an open source project, some maintain distribution packages, others run useful websites and some even attempt to found companies. I’ve had a few pet projects through my years.

Early in my high school education I discovered Microsoft Visual Basic 6.0. This was my original programming language and where I learnt the basis of my programming skills. Using Visual Basic I wrote many programs, some useful some utterly useless. I remember writing a chat program, a scrabble optimiser, a remote PC control application and several games. Unfortunately an over zealous system administrator saw many executables in my home directory and decided I had been infected by a virus and wiped the whole directory. Unfortunately as much as I protested and complained the files were never restored and they are all lost forever.

As I was completing my high school years I ran a web game with two fellow classmates of mine. We spend most of the second half of the year designing it and I spent my exam period implementing it. We managed to keep it running for a year until we ran out of funds (we were all studying) to support it. The game had several limitations and some major design flaws. I’ve entertained thoughts of setting it up once again many times, but ultimately without my two partners (one of who I’ve lost touch with) it would never work. In addition all copies of the original source code have been lost.

Now we come to my favourite project of all. WeatherMon was written for my Dad. He had bought a weather station that had a PC link and this enabled me to get the data into our server. Not only is WeatherMon‘s source code still available it is still running to this day. It was also my first foray into AJAX and XML. WeatherMon does not reload the page at all, and all data transferred is either images or XML. I’ve got a write up all about it here.

Finally, it began as a school assignment but I took it way too far. Originally I had implemented it as a web service, which I then extended to a website, then I made the XHTML so that it could easily be themed and finally I implemented several themes. You can read about converter here.

I think pet projects are what differentiates the passionate from the crowd. Anybody can write programs, and anybody can go to work and do it there. It takes the right person to want to toil outside hours on something that isn’t earning them any money. I think the best thing you can do to further your abilities and your career is to start a pet project. It doesn’t have to be thankless, or useless but that doesn’t mean it can’t be. Its easy, submit a patch to an open source project, become a maintainer for a project lacking development, fork a project, start a website or even start your own open source project.

Random Thought: How tasty is the definitive Open Sauce?