开发者

C++ calling child virtual member from parent virtual member

I create a parent class that calls it's own virtual member. But this virtual member is overridden by child class.

class Parent {
public:
    void doSomething() {
        doVirtual();
    }
protected:
    virtual voi开发者_如何学God doVirtual() {}
};

class Child : public Parent {
protected:
    virtual void doVirtual() {}
};

Parent *c = new Child();
c->doSomething();

And compile it with visual studio 2008, my question is: When i execute code from IDE (start debugging), it calls child method, but when i run executable directly, it calls parent method. Am i doing something wrong here?


If the functions don't do anything (or do exactly the same thing) how do you know that when you run the executable directly it calls the parent method?

Have the 2 functions actually do something different - the compiler might be 'coalescing' the functions if they're identical (though I'd expect that to be less likely to happen in a debug build).

If this answer makes no sense, post an exact (copy-n-paste) compilable snippet so we can see exactly what's happening.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜