virtual inheritance and virtual function.

A

Ashwin

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
 
R

Rolf Magnus

Ashwin said:
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.
 
P

Pierre Barbier de Reuille

Rolf said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top