M
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. 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.
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.