虚拟继承歧义 [英] Virtual Inheritance Ambiguity

查看:59
本文介绍了虚拟继承歧义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是c ++的新手,只是编写了一些代码来学习。我遇到了一个带有javaish设计问题的问题,想知道是否有任何可能的解决方案而不修改设计。我已经阅读了

虚拟继承,根据我的理解,这应该可行但是

我还没有找到任何使用这种纠结的例子的文档。包含错误的gcc

输出:


Example.cpp:在成员函数''virtual IObject * ArrayList :: Get(int)'':

Example.cpp:26:错误:''IObject''是''ArrayList''的模糊基础'

Example.cpp:在函数''int main(int ,char **)'':

Example.cpp:33:错误:无法分配抽象类型的对象

''ArrayList''

Example.cpp:23:注意:因为以下虚函数在''ArrayList''中是纯粹的


Example.cpp:4:注意:虚拟bool IObject :: Equals(IObject *)

Example.cpp:


class IObject

{

public:

virtual bool Equals(IObject * object)= 0;

};


class IList:public virtual IObject

{

public:

虚拟IObject * Get(int index)= 0;

};

class对象:public IObject

{

pub lic:

虚拟bool Equals(IObject * object){

返回此==对象;

}

};


类ArrayList:public Object,public IList

{

public:

virtual IObject * Get(int index){

返回此; //只需返回一些内容

}

};

int main(int argc,char * argv [])

{

IList * list = new ArrayList();

返回0;

}


谢谢。

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.

推荐答案

* mijobee:
* mijobee:

我'这是c ++的新手,只是编写一些代码来学习。我遇到了一个带有javaish设计问题的问题,想知道是否有任何可能的解决方案而不修改设计。
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.


我已经阅读了

虚拟继承,根据我的理解,这应该可行但是

我还没有找到任何使用这种纠结的例子的文档。包含错误的gcc

输出:
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;
Example.cpp:

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



您是否打算支持nullpointer参数?


如果没有,请将该参数作为参考。


并使其成为const的引用,并使Equals成为const成员函数。

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.


};


类IList:public virtual IObject

{

public:

virtual IObject * Get(int index)= 0;

};
};

class IList : public virtual IObject
{
public:
virtual IObject *Get(int index) = 0;
};



你是否打算在失败时返回null指针?


如果没有,请让它返回一个引用。


您可能也应该重载constness,并将名称更改为

反映它产生/做的事情,即


虚拟IObject& at(std :: size_t index)= 0;

虚拟IObject const& at(std :: size_t index)const = 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:

虚拟bool Equals(IObject * object){

返回此==对象;

}

};
class Object : public IObject
{
public:
virtual bool Equals(IObject *object) {
return this == object;
}
};



在这里实现Equals可能更好/没有/而不是在对象标识方面提供

实现,这将是对于大多数

类来说是错误的(但自实施以来,没有来自编译器的抗议)。

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 ,公共IList

{

public:

虚拟IObject * Get(int index){

返回此; //只返回一些东西

}
Example.cpp:在成员函数''virtual IObject * ArrayList :: Get(int)'':
Example.cpp:26:错误:''IObject''是''ArrayList'的模糊基础'
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有两个IObject子对象,一个是继承Object,
和一个通过IList的继承。使所有接口继承

虚拟。即''类对象:公共虚拟IObject''。

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();
};
int main(int argc, char *argv[])
{
IList *list = new ArrayList();


Example.cpp:在函数''int main(int,char **)''中:

示例。 cpp:33:错误:无法分配抽象类型的对象

''ArrayList''

Example.cpp:23:注意:因为以下虚函数是
纯''ArrayList'':

Example.cpp:4:注意:虚拟bool IObject :: Equals(IObject *)
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*)



因为编译ArrayList :: Get失败了,那个成员函数没有实现



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


返回0;
return 0;



不必要。

Unnecessary.


}
}



-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?

--
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?


mijobee写道:
mijobee wrote:

我是c +的新手,只是编写一些代码来学习。我遇到了一个带有javaish设计问题的问题,想知道是否有任何可能的解决方案而不修改设计。我已经阅读了

虚拟继承,根据我的理解,这应该可行但是

我还没有找到任何使用这种纠结的例子的文档。包含错误的gcc

输出:


Example.cpp:在成员函数''virtual IObject * ArrayList :: Get(int)'':

Example.cpp:26:错误:''IObject''是''ArrayList''的模糊基础'

Example.cpp:在函数''int main(int ,char **)'':

Example.cpp:33:错误:无法分配抽象类型的对象

''ArrayList''

Example.cpp:23:注意:因为以下虚函数在''ArrayList''中是纯粹的


Example.cpp:4:注意:虚拟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对象:public IObject

{
public:

virtual bool Equals(IObject * object){

return this == object;

}

};
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;
}
};



您还需要将IObject声明为Object的虚拟基类。注意

只有那些类真正存在才不会更多,只有那些继承为虚拟基类的
。在你的情况下,IObject将作为虚拟基类继承一次

,一次作为非虚基类继承,

导致两个(ambigous)基类。

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){

返回此; //只需返回一些东西

}

};


int main(int argc,char * argv [])

{

IList * list = new ArrayList();

返回0;

}


谢谢。
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.



问候,

Stuart

Regards,
Stuart


Alf P. Steinbach写道:


[剪断了原始问题和Alf的回答]


D ** n它,我只是想要成为第一个回答一次。你是

永远不会睡觉吗,Alf?

重新将其作为一个C ++问题:这是否可以保存以调用睡眠这个

好​​奇Alf对象?


Stuart
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


这篇关于虚拟继承歧义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆