CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2005
    Posts
    336

    Overrriding (interesting example)

    I see an article about this issue. There is a code:
    Code:
    class A
    {
    public:  
        virtual void foo(int n = 3)  
        {    
            std::cout << "A::foo(" << n << ")" << endl;  
        }
    };
    class B : public A
    {
    public:  
        virtual void foo()  
        {    std::cout << "B::foo" << endl;  
        }
    };
    
    int main()
    {  
        A* a = new B;  a->foo(); // prints A::foo("3");
    }
    Also i tried without virtual keyword, it also gives same result. But i don't understand why it is so.

    Code:
    class A
    {
    public:  
         void foo(int n = 3)  
        {    
            std::cout << "A::foo(" << n << ")" << endl;  
        }
    };
    class B : public A
    {
    public:  
         void foo()  
        {    std::cout << "B::foo" << endl;  
        }
    };
    
    int main()
    {  
        A* a = new B;  a->foo(); 
    	A as;
    	as.foo();
    
    }
    How can A see the foo() function?
    I can call as.foo() not as.foo(3); it doesnt give me error but it prints A::foo("3") but i didnt call a.foo(int n = 3);

    What does compiler do for such a situation when i inherite a class.
    A can see foo() function; is this normal? Because i know that function name resolution is only legal for scope. So A mustnt see foo() function. But it sees.

    Can anyone please explain this.

    Thanks.

  2. #2
    Join Date
    Apr 2002
    Location
    PA, USA
    Posts
    1,658

    Re: Overrriding (interesting example)

    This is almost word-for-word for how I see this example played out in a few of the "in depth" series books by A&W

    You need to use the using statement.

    class B : public A
    {
    public:
    using A::foo;
    void foo()
    { std::cout << "B::foo" << endl;
    }
    };
    =--=--=--=--=--=--=--=--=--=--=--=--=--=
    Please rate this post to show your appreciation for those that helped you.

    Before You Post A Question, Please Read This: How & When To Ask Your Question
    =--=--=--=--=--=--=--=--=--=--=--=--=--=

    -eli
    http://www.toad-software.com
    http://www.dailymission.com - Do It Daily

  3. #3
    Join Date
    Apr 2002
    Location
    PA, USA
    Posts
    1,658

    Re: Overrriding (interesting example)

    I found an example of how the full situation plays out:
    http://publib.boulder.ibm.com/infoce...se_derived.htm
    =--=--=--=--=--=--=--=--=--=--=--=--=--=
    Please rate this post to show your appreciation for those that helped you.

    Before You Post A Question, Please Read This: How & When To Ask Your Question
    =--=--=--=--=--=--=--=--=--=--=--=--=--=

    -eli
    http://www.toad-software.com
    http://www.dailymission.com - Do It Daily

  4. #4
    Join Date
    Sep 2005
    Posts
    336

    Re: Overrriding (interesting example)

    Ahh i made a silly mistake.
    as doesnt call B's function it also calls void foo(int n = 3). Because its parameter is int n = 3 not int n.
    Sorry and thanks Eli Gassert

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