关于名字查找

有人问我 CPP 在标识符前面加 :: 有什么用。直接上代码给回答:

#include <iostream>
using namespace std;
int a;
namespace tjua {
    int a;
    int *p=&::a; // :: 的用途
}

然后自己又想验证一下using在块作用域中的作用和全局是否一致,遂多打了几行:

namespace tjua{
    void f(int a);
}
void tjua::f(int a) {
    // using ::a; // error: target of using declaration conflicts with declaration already in scope
    // using namespace tjua; // neither error nor warning
    int *p1=&::a;
    int *p2=&tjua::a;
    int *p3=&a;
    cout << p1 << endl;
    cout << p2 << endl;
    cout << p3 << endl;
}

int main() {
    tjua::f(2); // output different Addr.
}

转载于:https://www.cnblogs.com/tjua/p/8268582.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值