472,353 Members | 1,150 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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 2533
* 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
Stuart Redmann wrote:
Alf P. Steinbach wrote:
>* Stuart Redmann:
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.
I guess I have to manually remove your memory banks one at a time until
only life support is operational :-)

Stu
But can Alf open the pod bay doors? Or should I just take a headache pill?
Oct 25 '06 #11

Alf P. Steinbach wrote in message <4q************@individual.net>...
>* 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.
But Alf, that would make them a "malicious joker"!! Going from windows to
GNU/Linux would be "practical"!! <G(Yes, I *am* ducking for cover.) <G>

--
Bob R
POVrookie
Oct 25 '06 #12
Thanks for all of your great feedback, sincerely appreciated. I made
are few changes, added another class String derived from Object, and
ran into a few more questions. The code below doesn't even compile but
my understanding was that c-style casts will always compile but may
cause major issues at runtime? My other question is about my use of
dynamic_cast, is this correct? I read some documentation that stated
using dynamic_cast to convert from base to derived was wrong unless the
base is polymorphic. What constitues being polymorphic, just having
some virtual methods? Thanks again everyone.
#include <stdio.h>

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

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

class ArrayList : public Object, public virtual IList
{
public:
virtual IObject *Get(int index) {
return this; // Just return something
}
};

class String : public Object { };
int main(int argc, char *argv[])
{
IList *list = new ArrayList();
IObject *obj = new String();
String *str = (String *)obj;
// This works: String *str = dynamic_cast<String *>(obj);
printf("str %s null.\n\0", str == 0 ? "is" : "is not");
}
Example.cpp: In function 'int main(int, char**)':
Example.cpp:39: error: cannot convert from base 'IObject' to derived
type 'String' via virtual base 'IObject'

Thanks again.
On Oct 25, 3:11 am, "Alf P. Steinbach" <a...@start.nowrote:
* 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 #13

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

Similar topics

9
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...
2
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
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...
4
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...
3
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...
2
by: Ashwin | last post by:
hi guys, can anyone explain this class Base { public: virtual void foo() = 0; virtual void bar() = 0; };
23
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...
1
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...
4
by: Anarki | last post by:
##include <iostream> using namespace std; class A { }; class B:virtual public A { }; class C:virtual public A
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.