CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 17 of 17
  1. #16
    Join Date
    Oct 2008
    Posts
    1,456

    Re: call derived method from base class

    I've not run your code on my machine, but the reason should be that the Base dtor is invoked after the Derived1 dtor. Therefore, the call to go() does not invoke Derived1::go. The same thing happens in the opposit direction when you try to invoke a virtual function in the ctor of the base. Indeed, try declaring Base1::go as pure virtual: you should see a pure virtual call exception in that case.

    EDIT: FYI, note that if you changed the code to something like

    Code:
    int main(int argc, char** argv){
        Derived1* der = new Derived1();
        der->start();
    
        std::cin.get();
    
        delete der;
    
        return 0;
    }
    it will appear to "work" again, because it's highly probable that go() gets invoked before der is deleted.
    Last edited by superbonzo; September 22nd, 2010 at 07:32 AM.

  2. #17
    Join Date
    Feb 2010
    Posts
    10

    Re: call derived method from base class

    ok thanks all for your help,

    and thanks superbonzo, it's just as you said

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured