Consider the following little program:
#include <iostream>
class Base {
public:
virtual void MyFunction() const {
std::cout << "This is Base" << std::endl;
}
};
class Derived : public Base {
public:
virtual void MyFuntcion() {
std::cout << "This is Derived" << std::endl;
}
};
int main() {
Base *p = new Derived();
p->MyFunction();
return 0;
}
it compiles cleanly with g++ -Wall -Wextra
with nary a peep from the compiler, but when you run it, it prints "This is Base
", due to the typo in the function name in Derived.
Now in java or C#, if you put an @override
tag on the function, you'll get a warning from the compiler about the typo. Is there any way to do something similar with gcc? Perhaps a magic __attribute__
or some such?