 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
mijobee Guest
|
Posted: Wed Oct 25, 2006 9:10 am Post subject: 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. |
|
| Back to top |
|
 |
Guest
|
Posted: Wed Oct 25, 2006 9:10 am Post subject: Re: Virtual Inheritance Ambiguity |
|
|
mijobee wrote:
| Quote: | 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 {
.....
} ; |
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Wed Oct 25, 2006 9:10 am Post subject: Re: Virtual Inheritance Ambiguity |
|
|
* Stuart Redmann:
| Quote: | 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.
| Quote: | 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? |
|
| Back to top |
|
 |
Stuart Redmann Guest
|
Posted: Wed Oct 25, 2006 9:10 am Post subject: Re: Virtual Inheritance Ambiguity |
|
|
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 |
|
| Back to top |
|
 |
Stuart Redmann Guest
|
Posted: Wed Oct 25, 2006 9:10 am Post subject: Re: Virtual Inheritance Ambiguity |
|
|
mijobee wrote:
| Quote: | 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.
| Quote: | 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 |
|
| Back to top |
|
 |
Stuart Redmann Guest
|
Posted: Wed Oct 25, 2006 9:10 am Post subject: Re: Virtual Inheritance Ambiguity |
|
|
Alf P. Steinbach wrote:
| Quote: | * 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 |
|
| Back to top |
|
 |
VJ Guest
|
Posted: Wed Oct 25, 2006 9:10 am Post subject: Re: Virtual Inheritance Ambiguity |
|
|
Alf P. Steinbach wrote:
| Quote: | * 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 |
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Wed Oct 25, 2006 9:10 am Post subject: Re: Virtual Inheritance Ambiguity |
|
|
* VJ:
| Quote: | 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? |
|
| Back to top |
|
 |
VJ Guest
|
Posted: Wed Oct 25, 2006 9:10 am Post subject: Re: Virtual Inheritance Ambiguity |
|
|
Alf P. Steinbach wrote:
| Quote: | 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( ) |
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Wed Oct 25, 2006 9:10 am Post subject: Re: Virtual Inheritance Ambiguity |
|
|
* mijobee:
| Quote: | 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.
| Quote: | 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.
| Quote: | 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.
| Quote: | };
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;
| Quote: | 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).
| Quote: | 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'.
| Quote: | };
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.
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? |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|