CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Threaded View

  1. #1
    Join Date
    May 2008
    Posts
    6

    vtable question: why its own method is called rather than the parent's one?

    class B derives from class A,
    A has two virtual methods: f1( ) and f2( ). In A::f1( ), it call f2( ).
    B override both virtual methods. In B::f1( ), it call A::f1( ). The question is: why B::f2( ) is called rather than A::f2( ).

    Thanks!


    Code:
    #include <stdio.h>
    
    class A
    {
        public: virtual void f1()
        {
            f2();
        }
    
        public: virtual void f2()
        {
            printf("A.f2()\n");
        }
    };
    
    class B : A
    {
        public: virtual  void f1()
        {
            A::f1();
        }
        public: virtual void f2()
        {
            printf("B.f2()\n");
        }
    };
    
    int main(int argc, char** argv)
    {
        B * b = new B();
        b->f1();
        delete b;
    }
    The output is:
    >> B.f2()
    Last edited by haiyeong; May 30th, 2008 at 08:19 PM. Reason: typo

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