Another C++ surprise
May. 6th, 2017 03:55 pmDid you know that the following code will compile?
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().
#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().