Hi there, I just found a possibly big mistake in my C++ understanding.
In below code, BBB:: print overwrites AAA:: print which is virtual, but there is no virtual keyword in BBB. Does this mean calling to print via a BBB pointer is static binding? I supposed so before today, but just now below sample program did not run as I expected.
It's known to me BBB:: print will overwrite AAA:: print, and my question is, will BBB:: print still keep the virtual attribute so that CCC:: print will also overwite it? I cannot believe I have such a big mis-understanding.
Thanks for your comments.
Code:
#include <stdio.h>
struct AAA
{
virtual void print(void)
{
printf("%s\n", __PRETTY_FUNCTION__);
}
};
struct BBB : public AAA
{
void print(void)
{
printf("%s\n", __PRETTY_FUNCTION__);
}
void print(void) const
{
printf("%s\n", __PRETTY_FUNCTION__);
}
};
struct CCC : public BBB
{
void print(void)
{
printf("%s\n", __PRETTY_FUNCTION__);
}
};