Emil’s Chronicle - The journal of Emil A Eklund

What’s so wrong with C++? Part 1

Preamble
How come everyone seems to hate C++ so much?

Over the past few months I’ve encountered quite a few articles criticizing C++, C/C++ and OOP in general. Initially I just got annoyed, ignored it and moved on, but it’s hard to ignore something that’s mentioned repeatedly, especially if comes from very intelligent persons.
This turned out to be a much longer article than I had expected, and I haven’t even finished it yet. So I’ve decided to split it into three parts, this being the first.

Scope
This article discusses object oriented languages in general and C++ in particular. For comparison procedural and other object oriented languages are used. No effort is made to compare it against functional, logical, relational or any other type of languages.

Introduction
Basically I believe people who dislike C++ generally falls into one of these categories:

  • Those who like straight forward procedural languages, like C, and reject the concept of OOP altogether.
  • Those who love OOP but prefer higher level languages, they don’t want to worry about memory handling, pointer arithmetics etc.
  • Those who think C++ is too complex.

I’ll discuss each of these three in turn, starting with the first one today, the other will follow in part 2 and 3.
Before that, however, I’d like to say a few words about the basics of Object Oriented Programming, or OOP for short,

Object Oriented Basics
For those of you that are not very familiar with OOP I’ll try to describe it, briefly. If you’re already accustomed to it you can safely skip this section.

The basic concept of OOP can be explained with the following two sentences:

  • It’s used to encapsulate a group of functions and variables that has something in common.
  • It allows inheritance, so two (or more) similar objects can share definition and/or code.

Might seem a bit oversimplified, but think about it, that’s pretty much all there is too it.

Encapsulation:
The concept of encapsulating related variables is not something that’s OOP specific, most languages allows this in one form or another.
C, for instance, has something called a struct which, as the name implies, is used to define a structure, a group, of variables and assign a name to it. A struct, however, cannot contain functions. An object, on the other hand, can. That’s the main difference, as far as encapsulation goes, between object oriented and procedural languages.

Inheritance/Polymorphism:
The other major difference between object oriented and procedural languages is that object oriented ones support inheritance.
That is, an object can inherit parts (or all) of the definition and/or implementation from another object. It’s not limited to a single level of inheritance, all OO languages by definition supports objects to inherit in a tree like structure, where object B inherits from A, and C from B.
Some languages, like C++, go even further and allow multiple inheritance, that is, object C inherits directly from A and B, where neither A nor B inherits from each other.
Whatever multiple inheritance is good thing or not (I think it is) is outside the scope of this article.

Polymorphism is, strictly speaking, the ability to redefine, or overload, methods for derived classes.
To use a classic example; Given a base class Shape, polymorphism enables the programmer to define different implementations for the getArea() method for each derived class, such as Rectangle, Triangle or Circle. When the getArea() method is called, no matter what shape the object is, the correct area for that shape will be returned.

Procedural vs Object Oriented
OOP is certainly not suitable for all programming tasks. It can be abused, and often is, and it offers no advantages on itself.

It can, however, make an awful lot of sense, when used correctly.

One of the main advantages of OOP is polymorphism; the ability to separate common code from multiple objects into an abstract base class, using virtual functions and inheritance. While this is often unnecessary for simple tasks more complex applications usually does this one way or another. In procedural languages, such as C, such implementations are often quite complex and increasingly tries to duplicate the functionality provided by OO languages.

Furthermore, most libraries takes an object oriented approach to data manipulation, whatever they’re written in an OO language or not. In procedural languages this is usually accomplished by passing a pointer to a struct as the first parameter. - As a site note, this is how OO is implemented in perl, foo->bar() is mapped to bar(foo). - A perfect example for this is the GD graphics library.

Consider the following C code.

gdImagePtr im;
int white;
 
im = gdImageCreate(64, 64);
white = gdImageColorAllocate(im, 255, 255, 255);
gdImageLine(im, 0, 0, 63, 63, white);
gdImageDestroy(im);

Now compare this to what it might have looked like where GD written in C++

GDImage im;
int white;
 
im = new GDImage(64, 64);
white = im->colorAllocate(255, 255, 255);
im->line(0, 0, 63, 63, white);
delete img;

Quite similar, don’t you think? In this case what strikes me as the biggest advantage is that the names of the member functions only needs to be unique for the GDImage class, not for the entire application. So to draw a line the method can be called line, rather than gdImageLine, the original function name. Another clear advantage is that objects are created using the standard new operator, and deleted using delete. No need for special functions for that.
It could even be created without the need for dynamic memory allocation altogether, like this:

GDImage im(64, 64);
int white = im.colorAllocate(255, 255, 255);
im.line(0, 0, 63, 63, white);

Who said memory management in C++ had to be hard? :)

To be continued…

Some of the articles that inspired this
OOP Is Much Better in Theory Than in Practice
http://www.devx.com/DevX/Article/26776

Let’s outlaw C++
http://youngpup.net/2004/1020011601

One Reason C++ Sucks Compared to C
http://nothings.org/computer/cpp.html

The C vs C++ vs C# vs Java vs Python language discussion that regularly pop up at planet gnome
http://planet.gnome.org/
http://ometer.com/desktop-language.html
http://www.artima.com/forums/flat.jsp?forum=106&thread=39226

2 Responses to 'What’s so wrong with C++? Part 1'

  1. David J Andersson Says:

    Hello Emil!
    Nice site you got here :) I relay like your article about C++ and OOP. What are you up to nowdays? Realy great writing. I’ve just noticed afew typeo’s
    “especially if comes from very intelligent persons.”
    “delete img;”

    Drop me a line sometime!

    Cheers

  2. Emil A Eklund Says:

    Thanks.
    Same old basically, what about you?

    Talking about typos; I guess the old saying “If you live in a glass house, don’t throw rocks” is applicable here, wouldn’t you agree? :)