Overriding virtual methods

Hi,

I've defined an abstract class IBase with a single method
1
2
3
 
  virtual bool foo(){ printf(" IBase \n"); }


I've also defined a derived class TestDerived:IBase that overrides this method

1
2
3

  bool foo(){ printf(" TestDerived \n");}


Finally I've a static lib with a function to use a IBase class

 
 bool doSomething(IBase * foo);


implemented inside the library :

 
  bool doSomething(IBase * foo){ foo->foo(); }


And this is the main program :

1
2
3
4
5
6
7
8
#include "mystaticlib.h"

int main(){
 IBase * ptr = new TestDerived();
 ptr->foo();
 doSomething(ptr);
}


I was especting an output like :

TestDerived
TestDerived


But the output is

TestDerived
IBase

Why inside the lib, the ptr is accessing to the method of the base?


Thanks in advance.
Line 4: IBase *ptr = new TestDerived(); is not meant to have the "()"

Also, an abstract class is a class where at least one pure virtual functon exists i.e. virtual bool foo() = 0;
The above comments are true, but shouldn't stop the thing from working.

I tried it on Visual Studio and it works, so you must have a build problem.
I recommend posting the entire class declarations. Perhaps you mistyped something while typing in the pieces of the program. Have you tried to duplicate the problem in a simpler application? Instead of posting just a main function, post an entire compilable program. If you can't duplicate the problem with a single executable (no libraries) then it could be something to do with the API implemented within the static library but I don't know why that would be a problem. What's the specific compiler you are using?
Topic archived. No new replies allowed.