467,907 Members | 1,370 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,907 developers. It's quick & easy.

virtual inheritance and virtual function.


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

Aug 1 '06 #1
  • viewed: 1573
Share:
2 Replies
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.

Aug 1 '06 #2
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
Aug 1 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Michael Winter | last post: by
18 posts views Thread by nenad | last post: by
4 posts views Thread by JKop | last post: by
20 posts views Thread by Daniel | last post: by
3 posts views Thread by Imre | last post: by
3 posts views Thread by kikazaru | last post: by
2 posts views Thread by Heinz Ketchup | last post: by
12 posts views Thread by mijobee | last post: by
23 posts views Thread by Dave Rahardja | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.