开发者

C++ calling child virtual member from parent virtual member

开发者 https://www.devze.com 2023-01-11 16:46 出处:网络
I create a parent class that calls it\'s own virtual member. But this virtual member is overridden by child class.

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

精彩评论

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

关注公众号