C ++中的函数隐藏和使用声明 [英] Function hiding and using-declaration in C++

查看:87
本文介绍了C ++中的函数隐藏和使用声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的困惑来自"C ++ Primer 5th Edition"第13.3节,第518页.

My confusion comes from "C++ Primer 5th edition" section 13.3, page 518.

非常细心的读者可能会怀疑为什么swap中的using声明没有隐藏swapHasPtr版本的声明.

Very careful readers may wonder why the using declaration inside swap does not hide the declarations for the HasPtr version of swap.

我试图阅读其参考文献,但仍然不明白为什么.有人可以解释一下吗?谢谢.这是问题的代码示例.

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

为什么HasPtrswap没有隐藏,而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屋!

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