c ++中的隐藏规则是什么? [英] What is the hiding rule in c++?
问题描述
我对名称隐藏"和信息隐藏"一词感到困惑.最重要的是,c ++中的隐藏规则是什么?有人可以给我一个定义吗?
I am so confused with the term name hiding and information hiding. Most importantly, What is the hiding rule in c++? Can someone give me a definition?
推荐答案
隐藏名称会在您覆盖某个类时发生:
Name hiding happens when you override a class:
struct A
{
int x;
int y;
void foo();
void bar();
};
struct B : A
{
int y;
void bar();
};
在类 B
中,名称 x
和 foo
是明确的,并且引用基类中的名称.但是,名称 y
和 bar
隐藏是基本名称.请考虑以下内容:
In class B
, the names x
and foo
are unambiguous and refer to the names in the base class. However, the names y
and bar
hide the base names. Consider the following:
B b;
b.bar();
函数名称指的是函数 B :: bar
的名称,因为基名是隐藏的,没有歧义.如果需要基本功能,则必须明确地说出它: b.A :: bar()
.另外,您可以在类定义中添加 using A :: bar;
来取消隐藏名称,但随后必须处理歧义(如果存在明显的超载).
The function name refers to the name of the function B::bar
without ambiguity, since the base name is hidden. You have to say it explicitly if you want the base function: b.A::bar()
. Alternatively, you can add using A::bar;
to your class definition to unhide the name, but then you have to deal with the ambiguity (this makes sense if there are distinct overloads, though).
最常见的名称隐藏示例之一是 operator =
的示例,它存在于每个类中,并且隐藏任何基类运算符;因此,为每个类都提供了一个隐式的运算符定义,而该定义没有提供显式的定义,如果您已经在基类中定义了一个赋值运算符,这可能会令人惊讶.
Perhaps one of the most frequently confusing examples of name hiding is that of the operator=
, which exists in every class and hides any base class operator; consequently, an implicit definition of the operator is produced for each class which doesn't provide an explicit one, which may come as a surprise if you already defined an assignment operator in a base class.
这篇关于c ++中的隐藏规则是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!