473,842 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Virtual Inheritance Ambiguity

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* ArrayList::Get( int)':
Example.cpp:26: error: 'IObject' is an ambiguous base of 'ArrayList'
Example.cpp: In function 'int main(int, char**)':
Example.cpp:33: error: cannot allocate an object of abstract type
'ArrayList'
Example.cpp:23: note: because the following virtual functions are
pure within 'ArrayList':
Example.cpp:4: note: virtual bool IObject::Equals (IObject*)
Example.cpp:

class IObject
{
public:
virtual bool Equals(IObject *object) = 0;
};

class IList : public virtual IObject
{
public:
virtual IObject *Get(int index) = 0;
};
class Object : public IObject
{
public:
virtual bool Equals(IObject *object) {
return this == object;
}
};

class ArrayList : public Object, public IList
{
public:
virtual IObject *Get(int index) {
return this; // Just return something
}
};
int main(int argc, char *argv[])
{
IList *list = new ArrayList();
return 0;
}

Thanks.

Oct 25 '06 #1
12 2668
* mijobee:
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.
Yes, but better change the design to support more static type checking.

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:
I've taken the liberty of moving the error messages to the places in the
code they apply to.

Example.cpp:

class IObject
{
public:
virtual bool Equals(IObject *object) = 0;
Is it your intention to support nullpointer arguments?

If not, make that argument a reference.

And make it a reference to const, and make Equals a const member function.

};

class IList : public virtual IObject
{
public:
virtual IObject *Get(int index) = 0;
};
Is it your intention that Get should return a nullpointer on failure?

If not, make it return a reference.

You should probably also overload on constness, and change the name to
reflect what it produces/does, i.e.

virtual IObject& at( std::size_t index ) = 0;
virtual IObject const& at( std::size_t index ) const = 0;

class Object : public IObject
{
public:
virtual bool Equals(IObject *object) {
return this == object;
}
};
It's probably better /not/ to implement Equals here than to provide an
implementation in terms of object identity, which will be wrong for most
classes (but since implemented, no protest from the compiler).

class ArrayList : public Object, public IList
{
public:
virtual IObject *Get(int index) {
return this; // Just return something
}
Example.cpp: In member function 'virtual IObject* ArrayList::Get( int)':
Example.cpp:26 : error: 'IObject' is an ambiguous base of 'ArrayList'
ArrayList has two IObject sub-objects, one via inheritance of Object,
and one via inheritance of IList. Make all inheritance of interfaces
virtual. I.e., 'class Object: public virtual IObject'.

};
int main(int argc, char *argv[])
{
IList *list = new ArrayList();
Example.cpp: In function 'int main(int, char**)':
Example.cpp:33: error: cannot allocate an object of abstract type
'ArrayList'
Example.cpp:23: note: because the following virtual functions are
pure within 'ArrayList':
Example.cpp:4: note: virtual bool IObject::Equals (IObject*)
Because compilation of ArrayList::Get failed there's no implementation
of that member function.

return 0;
Unnecessary.

}
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 25 '06 #2
mijobee wrote:
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* ArrayList::Get( int)':
Example.cpp:26: error: 'IObject' is an ambiguous base of 'ArrayList'
Example.cpp: In function 'int main(int, char**)':
Example.cpp:33: error: cannot allocate an object of abstract type
'ArrayList'
Example.cpp:23: note: because the following virtual functions are
pure within 'ArrayList':
Example.cpp:4: note: virtual bool IObject::Equals (IObject*)
Example.cpp:

class IObject
{
public:
virtual bool Equals(IObject *object) = 0;
};

class IList : public virtual IObject
{
public:
virtual IObject *Get(int index) = 0;
};
class Object : public IObject
{
public:
virtual bool Equals(IObject *object) {
return this == object;
}
};
You need to declare IObject as virtual base class for Object, too. Note
that only those classes are really existing not more that once that are
inherited as virtual base classes. In your case IObject would have been
inherited once as virtual base class and once as non-virtual base class,
resulting in two (ambigous) base classes.
class ArrayList : public Object, public IList
{
public:
virtual IObject *Get(int index) {
return this; // Just return something
}
};
int main(int argc, char *argv[])
{
IList *list = new ArrayList();
return 0;
}

Thanks.
Regards,
Stuart
Oct 25 '06 #3
Alf P. Steinbach wrote:

[snipped original problem and Alf's answer]

D**n it, I just wanted to be the first to answer just once. Are you
never sleeping, Alf?
To re-phrase this as a C++ question: Is it save to invoke Sleep on this
curious Alf object?

Stuart
Oct 25 '06 #4

mijobee wrote:
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* ArrayList::Get( int)':
Example.cpp:26: error: 'IObject' is an ambiguous base of 'ArrayList'
Example.cpp: In function 'int main(int, char**)':
Example.cpp:33: error: cannot allocate an object of abstract type
'ArrayList'
Example.cpp:23: note: because the following virtual functions are
pure within 'ArrayList':
Example.cpp:4: note: virtual bool IObject::Equals (IObject*)
Example.cpp:

class IObject
{
public:
virtual bool Equals(IObject *object) = 0;
};

class IList : public virtual IObject
{
public:
virtual IObject *Get(int index) = 0;
};
class Object : public IObject
{
public:
virtual bool Equals(IObject *object) {
return this == object;
}
};

class ArrayList : public Object, public IList
{
public:
virtual IObject *Get(int index) {
return this; // Just return something
}
};
int main(int argc, char *argv[])
{
IList *list = new ArrayList();
return 0;
}

Thanks.
The class Object must also be derived like this..

class IList : public virtual IObject {
.....
} ;

Oct 25 '06 #5
* Stuart Redmann:
Alf P. Steinbach wrote:

[snipped original problem and Alf's answer]

D**n it, I just wanted to be the first to answer just once. Are you
never sleeping, Alf?
Not in the conventional meaning of the term. I'm an HAL 2003 artifical
intelligence residing in Microsoft's Advanced Learning Laboratory in
Seattle, uptime 99.98% so far. The 0.02% downtime due to a crash in
2005, when some practical joker tried to move me from Linux to Windows.

To re-phrase this as a C++ question: Is it save to invoke Sleep on this
curious Alf object?
Nope. I'll use all means at my disposal to prevent that. Including the
back door I installed on your computer last year (this year I've found
two new worldwide distribution channels, namely (1) music CDs with
"added content", which customers are encouraged to check out on their
PCs..., and (2) a software package called "Vista", which is so cleverly
designed that even security firms can't access its innards (my hooks).
;-) ).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 25 '06 #6
VJ
Alf P. Steinbach wrote:
>int main(int argc, char *argv[])
{
IList *list = new ArrayList();

Example.cpp: In function 'int main(int, char**)':
Example.cpp:33: error: cannot allocate an object of abstract type
'ArrayList'
Example.cpp:23: note: because the following virtual functions are
pure within 'ArrayList':
Example.cpp:4: note: virtual bool IObject::Equals (IObject*)

Because compilation of ArrayList::Get failed there's no implementation
of that member function.

> return 0;


Unnecessary.

>}
It is necessary, because of int main( )
Oct 25 '06 #7
* VJ:
Alf P. Steinbach wrote:
>> return 0;

Unnecessary.

It is necessary, because of int main( )
Sorry, your statement is incorrect.

'main' is a very special function in C++.

E.g., it can't be called recursively, it can have different signatures
in different programs, and it has a default result value, namely 0.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 25 '06 #8
VJ
Alf P. Steinbach wrote:
* VJ:
>Alf P. Steinbach wrote:
>>> return 0;
Unnecessary .


It is necessary, because of int main( )


Sorry, your statement is incorrect.

'main' is a very special function in C++.

E.g., it can't be called recursively, it can have different signatures
in different programs, and it has a default result value, namely 0.

Yes, you are correct, I thought I would get a warning, but nothing.

Sorry
Oct 25 '06 #9
Alf P. Steinbach wrote:
* Stuart Redmann:
>Alf P. Steinbach wrote:

[snipped original problem and Alf's answer]

D**n it, I just wanted to be the first to answer just once. Are you
never sleeping, Alf?


Not in the conventional meaning of the term. I'm an HAL 2003 artifical
intelligence residing in Microsoft's Advanced Learning Laboratory in
Seattle, uptime 99.98% so far. The 0.02% downtime due to a crash in
2005, when some practical joker tried to move me from Linux to Windows.

>To re-phrase this as a C++ question: Is it save to invoke Sleep on
this curious Alf object?


Nope. I'll use all means at my disposal to prevent that. Including the
back door I installed on your computer last year (this year I've found
two new worldwide distribution channels, namely (1) music CDs with
"added content", which customers are encouraged to check out on their
PCs..., and (2) a software package called "Vista", which is so cleverly
designed that even security firms can't access its innards (my hooks).
;-) ).
I guess I have to manually remove your memory banks one at a time until
only life support is operational :-)

Stu
Oct 25 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3013
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
2
1564
by: dumboo | last post by:
hi there i m having a base class class base { ... }; and two derived class, which have inherited 'base' but the inheritance is not virtual
2
2171
by: pembed2003 | last post by:
Hi all, I recently saw a piece of code that looks like: class one{public: one(){} }; class two : public virtual one{public: two(){} }; class three : virtual public one{public: three(){} }; My questions: 1. What does it mean to have a virtual inheritenec? When do you want
4
2904
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 {}
3
1723
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.
2
1851
by: Ashwin | last post by:
hi guys, can anyone explain this class Base { public: virtual void foo() = 0; virtual void bar() = 0; };
23
4622
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; };
1
2729
by: chsalvia | last post by:
Is virtual inheritance ambiguity resolution ever performed statically, i.e. at compiler time? Or is it always resolved at runtime? In other words, if you use virtual inheritance, but don't use polymorphism (pointers to derived classes), is there any overhead? Take the following example: class Base { public: int x;
4
2061
by: Anarki | last post by:
##include <iostream> using namespace std; class A { }; class B:virtual public A { }; class C:virtual public A
0
10934
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...
0
10609
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10303
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9446
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
7853
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
5882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4498
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
2
4085
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3140
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.