virtual inheritance and virtual function.

Discussion in 'C++' started by Ashwin, Aug 1, 2006.

  1. Ashwin

    Ashwin Guest

    hi guys,

    can anyone explain this

    class Base {
    public:
    virtual void foo() = 0;
    virtual void bar() = 0;
    };

    class Der1 : public virtual Base {
    public:
    virtual void foo();
    };

    void Der1::foo()
    { bar(); }

    class Der2 : public virtual Base {
    public:
    virtual void bar();
    };

    class Join : public Der1, public Der2 {
    public:
    ...
    };

    int main()
    {
    Join* p1 = new Join();
    Der1* p2 = p1;
    Base* p3 = p1;

    p1->foo();
    p2->foo();
    p3->foo();
    }
    when Der1::foo() calls this->bar(), it ends up calling Der2::bar().
    How does this happen .Please explain the mechanism.

    Thanks and Regards
    Ashwin
     
    Ashwin, Aug 1, 2006
    #1
    1. Advertising

  2. Ashwin

    Rolf Magnus Guest

    Ashwin wrote:

    >
    > hi guys,
    >
    > can anyone explain this
    >
    > class Base {
    > public:
    > virtual void foo() = 0;
    > virtual void bar() = 0;
    > };
    >
    > class Der1 : public virtual Base {
    > public:
    > virtual void foo();
    > };
    >
    > void Der1::foo()
    > { bar(); }
    >
    > class Der2 : public virtual Base {
    > public:
    > virtual void bar();
    > };
    >
    > class Join : public Der1, public Der2 {
    > public:
    > ...
    > };
    >
    > int main()
    > {
    > Join* p1 = new Join();
    > Der1* p2 = p1;
    > Base* p3 = p1;
    >
    > p1->foo();
    > p2->foo();
    > p3->foo();
    > }
    > when Der1::foo() calls this->bar(), it ends up calling Der2::bar().
    > How does this happen .Please explain the mechanism.


    Well, the C++ standard doesn't define the mechanism behind virtual
    inheritance, so it's compiler specific.
     
    Rolf Magnus, Aug 1, 2006
    #2
    1. Advertising

  3. Rolf Magnus wrote:
    > Ashwin wrote:
    >
    >> hi guys,
    >>
    >> can anyone explain this
    >>
    >> class Base {
    >> public:
    >> virtual void foo() = 0;
    >> virtual void bar() = 0;
    >> };
    >>
    >> class Der1 : public virtual Base {
    >> public:
    >> virtual void foo();
    >> };
    >>
    >> void Der1::foo()
    >> { bar(); }
    >>
    >> class Der2 : public virtual Base {
    >> public:
    >> virtual void bar();
    >> };
    >>
    >> class Join : public Der1, public Der2 {
    >> public:
    >> ...
    >> };
    >>
    >> int main()
    >> {
    >> Join* p1 = new Join();
    >> Der1* p2 = p1;
    >> Base* p3 = p1;
    >>
    >> p1->foo();
    >> p2->foo();
    >> p3->foo();
    >> }
    >> when Der1::foo() calls this->bar(), it ends up calling Der2::bar().
    >> How does this happen .Please explain the mechanism.

    >
    > Well, the C++ standard doesn't define the mechanism behind virtual
    > inheritance, so it's compiler specific.
    >


    Well, according to the norm, here is the (complex) "mechanism" used to
    determine which function will be called:

    """
    The following steps define the result of name lookup in a class scope,
    C. First, every declaration for the name in the class and in each of its
    base class sub-objects is considered. A member name f in one subobject
    B hides a member name f in a sub-object A if A is a base class
    sub-object of B. Any declarations that are so hidden are eliminated from
    consideration. Each of these declarations that was introduced by a
    using-declaration is considered to be from each sub-object of C that is
    of the type containing the declaration designated by the
    using-declaration.96) If the resulting set of declarations are not all
    from sub-objects of the same type, or the set has a nonstatic member and
    includes members from distinct sub-objects, there is an ambiguity and
    the program is ill-formed. Otherwise that set is the result of the lookup.
    """

    You can note that the virtual keyword do not influence the result at all !!!

    Thus, as you are using polymorphism, "foo" is the same whatever the base
    type you are considering (this is the use of virtual functions ...).

    Pierre
     
    Pierre Barbier de Reuille, Aug 1, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page