C ++中的函数隐藏和使用声明 [英] Function hiding and using-declaration in C++
问题描述
我的困惑来自"C ++ Primer 5th Edition"第13.3节,第518页.
My confusion comes from "C++ Primer 5th edition" section 13.3, page 518.
非常细心的读者可能会怀疑为什么
swap
中的using
声明没有隐藏swap
的HasPtr
版本的声明.
Very careful readers may wonder why the
using
declaration insideswap
does not hide the declarations for theHasPtr
version ofswap
.
我试图阅读其参考文献,但仍然不明白为什么.有人可以解释一下吗?谢谢.这是问题的代码示例.
I tried to read its reference but still did not understand why. Could anyone explain it a little bit please? Thanks. Here is the code sample of the question.
假定类Foo
具有一个名为h
的成员,该成员的类型为HasPtr
.
Assume class Foo
has a member named h
, which has type HasPtr
.
void swap(HasPtr &lhs, HasPtr &rhs)
{...}
void swap(Foo &lhs, Foo &rhs)
{
using std::swap;
swap(lhs.h, rhs.h);
}
为什么HasPtr
的swap
没有隐藏,而using std::swap
在内部范围中似乎在外部范围中声明了?谢谢.
Why swap
for HasPtr
is not hidden which seems to be declared in outer scope while using std::swap
is in the inner scope? Thanks.
推荐答案
因为using std::swap;
并不意味着此后,每个'交换'都应使用std::swap
"",而是将swap
的所有重载从
Because using std::swap;
does not mean "henceforth, every 'swap' should use std::swap
", but "bring all overloads of swap
from std
into the current scope".
在这种情况下,效果与您在函数内编写using namespace std;
的效果相同.
In this case, the effect is the same as if you had written using namespace std;
inside the function.
这篇关于C ++中的函数隐藏和使用声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!