views:

41

answers:

1

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 void 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?

A: 

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.

Michael Burr
I'm sorry, but the one I put here is simplest form from actual code. There is code there in child method, and just to made sure, I had put several std::cout there. And they are called when start from debugger (even on release mode), but fail if run directly from executable (release mode).It's only work when I cast it back to child pointer.
flamemyst
I'm sorry i can't reduce it to simple case or replay it misbehavior in another project, maybe a bug is creeping in my code.
flamemyst
@user422420: I'd suggest trying to completely clean your intermediate output (including pre-compiled headers) and rebuild to see if that helps at all.
Michael Burr
@Michael Brurr: i found mistakes, some uninitialized variable (bool for checking before calling doVirtual()) creeps in my parent code, so it never call doVirtual(). Thank you for your help!
flamemyst