友谊和继承有什么区别? [英] What's the difference between friendship and inheritance?
问题描述
假设有两个A和B类:
class A {};
class B {};
以下两个例子在哪些方面有所区别?
In what aspects differ the two examples below?
示例1:
class C : public A, public B {};
示例2:
class C
{
//private
friend class A;
friend class B;
}
推荐答案
A 朋友
可以触摸任何朋友的私人
部分(只是有点故意!;)),但没有任何 A
和 B
是 C
的一部分 - 它只是意味着 A
和 B
可以触及 C
的私有位)。 A
和 B $当然也可以少于
私人
。 c $ c>,所以如果 C
有受保护
或公开
会员,也将可用。
A friend
can touch the private
parts (pun only slightly intentional! ;) ) of whatever it is friend of, but nothing of A
and B
are part of C
- it just means that "A
and B
can touch C
's private bits"). Anything "less" than private
is of course also available to A
and B
, so if C
has protected
or public
members, that will also be available.
当你继承时, A
和 B
成为 C
的一部分。 A
和 B
的任何私有
部分均不可用于 C
。在is-a与has-a命名法中, C
现在是-$ B
- 换句话说,它继承自 A
,所以它表现得像从接口角度看
。
When you inherit, the A
and B
becomes part of C
. Any private
sections of A
and B
are not available to C
. In the "is-a" vs. "has-a" nomenclature, C
now is-a A
and is-a B
- in other words, it's inherited from A
, so it "behaves like A
from an interface perspective.
这篇关于友谊和继承有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!