Take a look at this small C++
program:
#include <iostream>
#define debugStream std::cout
struct Id {
Id() {
debugStream << this->getClassName() << " created\n";
}
virtual ~Id() {
debugStream << this->getClassName() << " destroyed\n";
}
virtual std::string getClassName() {
return "Id";
}
};
struct D : public Id {
std::string getClassName() {
return "D";
}
};
int main( int argc, const char *argv[] ) {
Id* i = new D();
return 0;
}
I expected this program to print:
D created
D destroyed
but instead it prints:
Id created
Id destroyed
virtual
is working as expected for the destructor but not for the getClassName
function. Why is it so?