Problem overridding virtual function

Posted by William on Stack Overflow See other posts from Stack Overflow or by William
Published on 2010-05-28T18:06:55Z Indexed on 2010/05/28 18:12 UTC
Read the original article Hit count: 207

Filed under:
|
|
|

Okay, I'm writing a game that has a vector of a pairent class (enemy) that s going to be filled with children classes (goomba, koopa, boss1) and I need to make it so when I call update it calls the childclasses respective update. I have managed to create a example of my problem.

#include <stdio.h>
class A{
    public:
        virtual void print(){printf("Hello from A");}
};

class B : public A{
    public:
        void print(){printf("Hello from B");}
};


int main(){
    A ab = B();
    ab.print();
    while(true){}
}

Output wanted: "Hello from B" Output got: "Hello from A"

How do I get it to call B's print function?

© Stack Overflow or respective owner

Related posts about c++

Related posts about c