C++ in Stevenage

I'm stuck in Stevenage all week teaching a C++ course to ten engineers. Right now I am dicking with the hired laptop while they do exercises. Occasionally I come up with interesting C++ nastinesses to show them.

Just now I was talking about a hypothetical overloaded operator+ function, and trying to show how the compiler translates uses of + into calls to that function. I wrote a method call on the whiteboard and someone asked me if you could actually use that type of call directly. Well, I thought not, but you can never be sure with C++, so I tried it. It turns out that this is entirely valid (according to G++ 3.0):

#include <iostream>
using namespace std;

class Foo
{
 public:
   int operator+ (int i) const {
      return 23;
   }
};

int main ()
{
   Foo f;

   cout << f.operator+(42) << endl;
}

Oh, getting close to lunchtime. If I can just look alert for a few more minutes I'll be able to sneak off for a cig :-/

Back from lunch: and one of the students suffers from a seg fault. I see nothing wrong for a while, but eventually spot a strange initialiser list where they've constructed a string object from the numeric literal 0. I ponder how this could work, and eventually realise that it's not a numeric literal, but a pointer literal. The compiler thinks this is fine, because a string object can be constructed from a char* value, and the token 0 can be interpreted as a null pointer of any pointer type. So much for type safety.

Just to illustrate, this is entirely valid, and compiles without warnings even in maximum paranoiac mode, but crashes because of a null pointer dereference:

#include <iostream>
#include <string>
using namespace std;

int main ()
{
   string foo (0);
   cout << foo << endl;
}

< Playing with mod_perl | Bach on console speaker >

Miniblog

(nuggets of inanity)

Tuesday Apr 24th 2007, 16:54 »
Just took the annual web design survey that AListApart do. I don't realy consider myself to be a web designer, but I have been doing a lot of HTML and CSS lately.
Monday Apr 23rd 2007, 18:23 »
Strange, there appears to be a bare-knuckle boxing match going on in the field outside my flat. Wish they wouldn't make so much noise about it.
Thursday Mar 1st 2007, 18:47 »
“In its written form, Hebrew has no vowels, making it the ideal language for texting.”
—Said in jest on some Radio 4 programme just now.

Archive: 2007 · 2006 · 2005 · 2004
Feed