473,795 Members | 3,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
2 1850
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
19338
by: Prabu | last post by:
Hi, I'm new to python, so excuse me if i'm asking something dumb. Does python provide a mechanism to implement virtual functions? Can you please give a code snippet also...:) Thanx in advance -Prabu.
9
3010
by: Michael Winter | last post by:
Until about 5 minutes ago, I was happy with my knowledge of virtual functions - then I read "Mixing interface and functional inheritance" posted by Kevin L. earlier today. All of a sudden, I found out that you can inherit using the virtual keyword: class A { } class B : public *virtual* A
18
2225
by: nenad | last post by:
Wouldn't it be nice if we could do something like this: class Funky{ public: auto virtual void doStuff(){ // dostuff } };
4
2901
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that there shouldn't have been a "virtual" keyword for this purpose, but instead, a "nonvirtual" keyword! In teaching inheritance, you see the common example: class Vehicle {}
20
2021
by: Daniel | last post by:
I have the following three classes class A { public: virtual void f() = 0; }; class B: public A {
3
1716
by: Imre | last post by:
Hi! I've got some questions regarding heavy use of virtual inheritance. First, let's see a theoretical situation, where I might feel tempted to use a lot of virtual inheritance. Let's suppose, we're creating a little strategy game. In our game, there are Units. A Unit can be either a Human, or a Vehicle. Obviously, Human and Vehicle are subclasses of Unit.
3
4552
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape = base class Triangle, Square = classes derived from Shape Prism = class derived from Shape TriangularPrism, SquarePrism = classes derived from Triangle and Prism, or Square and Prism respectively
2
1870
by: Heinz Ketchup | last post by:
Hello, I'm looking to bounce ideas off of anyone, since mainly the idea of using Multiple Virtual Inheritance seems rather nutty. I chalk it up to my lack of C++ Experience. Here is my scenario... I have 5 Derived Classes I have 3 Base Classes
12
2666
by: mijobee | last post by:
I'm very new to c++ and just writing some code to learn. I've run into a problem, with a javaish design, and want to know if there is any possible solution without modifying the design. I've read up on virtual inheritance and from my understanding this should work fine but I haven't found any docs that use such a tangled example. The gcc output containing the errrors: Example.cpp: In member function 'virtual IObject*...
23
4616
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
0
10437
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.