2017-05-06

torkell: (Default)
2017-05-06 03:55 pm
Entry tags:

Another C++ surprise

Did you know that the following code will compile?

#include <stdio.h>

class Foo {
public:
  Foo();
  ~Foo();

  void FrobSomething(void* something);
};

Foo::Foo() {
  printf("New foo!\n");
}

Foo::~Foo() {
  printf("Destroyed foo!\n");
}

void main() {
  Foo foo;
}


No, the surprise isn't the slightly incorrect definition of main(). The surprise is that the Foo class is declared as containing a FrobSomething() method, but there's no implementation of the method. C++ is perfectly happy with this as long as you never actually try to call FrobSomething().