int main()
{
DerivedClass* aDC = new DerivedClass();
vector<BaseClass*> aVec;
aVec.push_back( aDC );
aVec[0]->printStuff();
delete aDC;
return 0;
}
When I call printStuff, the DerivedClass's function gets called. Now, if I remove the const part from the DerivedClass's printStuff function, we call the BaseClass's printStuff function. Can anyone explain why this happens? I tried a Google search, but not quite sure how to word this.
If you do not declare the member function as const, then you are overloading, not overriding, the member function to have a non-const version. Thus, the polymorphism that comes with an override does not apply. I expect that the base class' version of the member function becomes hidden in the derived class (i.e., you would get an error if you tried calling it with a const DerivedClass object).
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar
No, it wasn't. I just said "...Now, if I remove the const part from the DerivedClass's printStuff function...". If I leave the code the way it the way is, things behave as I would expect them to. But, if I remove the const from the DerivedClass's function, then the BaseClass's printStuff function is called. I think laserlight's explanation makes sense.
Bookmarks