多重继承的问题 [英] problem with multiple inheritance

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

问题描述

我有两个班级,比如A和B,两个都有一个数据成员''int n'';

私人在A,公共在B.


当我从公共A和公共B中派生出C类时,B :: n应该是对C可见的
而A :: n不应该是。


但是如果我使用g ++ - 4.0.3编译以下代码片段:


A类{

int i;

public:

A(){}

};


B级{

公开:

int i;

B(){}

};


class C :公众A,公共B {

public:

C(){i = 3; }

};


int main(){

C c;

返回0;

}


我收到以下错误:


test.cpp:在构造函数''C :: C()'':

测试:15:错误:对''我'的引用含糊不清

测试:9:错误:候选者是:int B ::我是
测试:2:错误:int A :: i


没什么戏剧性的,当然我可以更改名称或使用访问权限

说明符。我只是想知道我是否误解了Stroustrup第2版的第6章

,或者这是编译问题。


感谢您的关注,


Daniele

I have two classes, say A and B, both having a data member ''int n'';
private in A, public in B.

When I derive class C from both public A and public B, B::n should be
visible to C while A::n should not be.

But if I compile with g++-4.0.3 the following snippet:

class A {
int i;
public:
A () {}
};

class B {
public:
int i;
B () {}
};

class C : public A, public B {
public:
C () { i = 3; }
};

int main () {
C c;
return 0;
}

I get the following error:

test.cpp: In constructor ''C::C()'':
test:15: error: reference to ''i'' is ambiguous
test:9: error: candidates are: int B::i
test:2: error: int A::i

Nothing dramatic, of course I can change the names or use access
specifiers. I just would like to know if I am misinterpreting chapter 6
of Stroustrup 2nd edition or if this is a compiler problem.

Thanks for the attention,

Daniele

推荐答案

dl写道:
dl wrote:

我有两个类,比如A和B,都有一个数据成员''int n'';
I have two classes, say A and B, both having a data member ''int n'';



''int i;''

''int i;''


私人在A,公共在B.


当我从公共A和公共B中派生出C类时,B :: n应该是C可见的
而A :: n不应该是。
private in A, public in B.

When I derive class C from both public A and public B, B::n should be
visible to C while A::n should not be.



它们都是可见的。但是,A :: i是*无法访问的*。规则

的可访问性和名称的可见性是正交的。首先,应用

可见性规则,然后验证访问*。在

你的情况下,两个具有相同名称和相同类型的变量

相互冲突。您需要通过限定名称来告诉编译器您要使用哪一个

:A :: i或B :: i。

They are both visible. A::i, however, is *inaccessible*. The rules
of accessibility and visibility of names are orthogonal. First, the
rules of visibility are applied, then the access is *verified*. In
your case two variables that have the same name and the same type
conflict with each other. You need to tell the compiler which one
you mean to use by qualifying the name: A::i or B::i.


>

但是如果我使用g ++ - 4.0.3编译以下代码片段:


A类{

int i ;

公开:

A(){}

};


B级{

public:

int i;

B(){}

};

class C:public A,public B {

public:

C(){i = 3; }

};


int main(){

C c;

返回0;

}


我收到以下错误:


test.cpp:在构造函数''C :: C()'':

测试:15:错误:对''我'的引用含糊不清

测试:9:错误:候选者是:int B ::我是
测试:2:错误:int A :: i


没什么戏剧性的,当然我可以更改名称或使用访问权限

说明符。
>
But if I compile with g++-4.0.3 the following snippet:

class A {
int i;
public:
A () {}
};

class B {
public:
int i;
B () {}
};

class C : public A, public B {
public:
C () { i = 3; }
};

int main () {
C c;
return 0;
}

I get the following error:

test.cpp: In constructor ''C::C()'':
test:15: error: reference to ''i'' is ambiguous
test:9: error: candidates are: int B::i
test:2: error: int A::i

Nothing dramatic, of course I can change the names or use access
specifiers.



错误的术语。语法''B ::''(或''A ::'')被称为嵌套名称

说明符。访问说明符是私有的。或者公共。

Wrong terminology. The syntax ''B::'' (or ''A::'') is called "nested name
specifier". Access specifier is the "private" or "public".


我只是想知道我是否误解了Stroustrup第二版的章节

6,或者这是编译问题。
I just would like to know if I am misinterpreting chapter
6 of Stroustrup 2nd edition or if this is a compiler problem.



最有可能。


V

-

请在通过电子邮件回复时删除大写''A'

我没有回复最热门的回复,请不要问

Most likely.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask

Victor Bazarov ha scritto:
Victor Bazarov ha scritto:

dl写道:
dl wrote:

我有两个班,比如说A和B,都有一个数据成员''int n'';
I have two classes, say A and B, both having a data member ''int n'';



''int i;''


''int i;''


私人在A,公共在B.


当我从公共A和公共B中派生出C类时,B :: n应该是C可见的
而A :: n不应该是。
private in A, public in B.

When I derive class C from both public A and public B, B::n should be
visible to C while A::n should not be.



它们都是可见的。但是,A :: i是*无法访问的*。规则

的可访问性和名称的可见性是正交的。首先,应用

可见性规则,然后验证访问*。在

你的情况下,两个具有相同名称和相同类型的变量

相互冲突。您需要通过限定名称告诉编译器您要使用哪一个

:A :: i或B :: i。


They are both visible. A::i, however, is *inaccessible*. The rules
of accessibility and visibility of names are orthogonal. First, the
rules of visibility are applied, then the access is *verified*. In
your case two variables that have the same name and the same type
conflict with each other. You need to tell the compiler which one
you mean to use by qualifying the name: A::i or B::i.



但是如果我使用g ++ - 4.0.3编译以下代码片段:


A类{

int i;

public:

A(){}

};


B级{

public:

int i;

B(){}

};


class C:公众A,公共B {

public:

C(){i = 3; }

};


int main(){

C c;

返回0;

}


我收到以下错误:


test.cpp:在构造函数''C :: C()'':

测试:15:错误:对''我'的引用含糊不清

测试:9:错误:候选者是:int B ::我是
测试:2:错误:int A :: i


没什么戏剧性的,当然我可以更改名称或使用访问权限

说明符。

But if I compile with g++-4.0.3 the following snippet:

class A {
int i;
public:
A () {}
};

class B {
public:
int i;
B () {}
};

class C : public A, public B {
public:
C () { i = 3; }
};

int main () {
C c;
return 0;
}

I get the following error:

test.cpp: In constructor ''C::C()'':
test:15: error: reference to ''i'' is ambiguous
test:9: error: candidates are: int B::i
test:2: error: int A::i

Nothing dramatic, of course I can change the names or use access
specifiers.



错误的术语。语法''B ::''(或''A ::'')被称为嵌套名称

说明符。访问说明符是私有的。或者公共。


Wrong terminology. The syntax ''B::'' (or ''A::'') is called "nested name
specifier". Access specifier is the "private" or "public".


我只是想知道我是否误解了Stroustrup第二版的章节

6,或者这是编译问题。
I just would like to know if I am misinterpreting chapter
6 of Stroustrup 2nd edition or if this is a compiler problem.



最有可能。


V

-

请在通过电子邮件回复时删除大写''A'

我没有回复最热门的回复,请不要问


Most likely.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask



谢谢。

Thank you.


我收到以下错误:
I get the following error:

> ;

test.cpp:在构造函数''C :: C()''中:

测试:15:错误:对''我'的引用不明确

测试:9:错误:候选人是:int B :: i

测试:2:错误:int A :: i


感谢您的关注,


Daniele
>
test.cpp: In constructor ''C::C()'':
test:15: error: reference to ''i'' is ambiguous
test:9: error: candidates are: int B::i
test:2: error: int A::i

Thanks for the attention,

Daniele



这不是一个编译器错误,而是一个不断的试验

多个固有(拼写?)...因为对象C是A和B的孩子,所以尽管事实上它仍然是变量i />
它不公开(有关更多信息,请查询关键字

''private''和''protected'')。我不建议使用多个

继承,除非你对这个概念更加友好(它还会为你的应用程序增加一些开销)。但是,如果你继续这样的话,我相信这里的问题可以通过使用

''virtual''关键字来解决。当你声明一个类''virtual''时,你是

说明当继承它时,对象应该继承所有的值,除非它们因某种原因已经存在(或多或少)。 ..大多数少)。什么

你应该做的是创建另一个虚拟类(比方说E)

包含i,让A和B继承自E然后让C继承
A和B的
。希望有帮助,不要反驳或混淆......

-Leif902
http://www.greenmangames.vze.com


(如果你不喜欢)不想做所有这些,你可以参考

类...比如''cA :: i'',但这并不是一个非常好的解决方案,因为它

强制将类信息放在应用程序代码中,这是一个很大的不... ...

This is not a compiler error, rather it is a constant plauge on
multiple inheretance (spelling?)... Because the object C is a child of
both A and B, it inherents variable i from both despite the fact that
it is not made public (for more information, look up the keywords
''private'' and ''protected''). I would not recommend using multiple
inheritance unless you are more comforatable with the concept (it also
adds a bit of overhead to your applications). However, if you would
like tho continue, I beleive the problem here can be resolved by use of
the ''virtual'' keyword. When you declare a class ''virtual'' you are
stating that when inherited it objects should inherit all values unless
they already exist for some reason (more or less... mostly less). What
you should probably do is create another virtual class (lets say E)
that contains i, have A and B inherit from E and then have C inherit
from A and B. Hope that helps and isn''t to vauge or confusing...
-Leif902
http://www.greenmangames.vze.com

(or if you don''t want to do all that, you can just reference the
class... like ''c.A::i'', this however is not a very good solution as it
forces class information to be placed in application code which is a
big no...)


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

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