- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
下面的 friend
函数不是通过普通查找 (§7.3.1.2/3) 找到的,而是通过 ADL 找到的 (§3.4.2/4 第二个要点),因此代码编译通过并正常执行(live example)。但是函数 f
没有在任何命名空间中声明。例如,如果您尝试将调用 f(x);
替换为这些调用中的任何一个 ::f(x);
,A::f(x );
或 A::X::f(x);
,代码将无法编译。哪个命名空间包含这个友元函数的声明?标准对此有何规定?
#include <iostream>
namespace A {
class X {
int i;
friend void f(X x) { std::cout << x.i << '\n'; }
public:
X():i(101){}
};
}
int main()
{
A::X x;
f(x);
}
最佳答案
来自 C++ 标准
11.3 friend
6 A function can be defined in a friend declaration of a class if and only if the class is a non-local class (9.8), the function name is unqualified, and the function has namespace scope. [ Example:
class M { friend void f() { } // definition of global f, a friend of M,
// not the definition of a member function
};
—end example ]
Amd 其他引用(7.3.1 命名空间定义)
3 Every name first declared in a namespace is a member of that namespace. If a friend declaration in a non-local class first declares a class, function, class template or function template98 the friend is a member of the innermost enclosing namespace. The friend declaration does not by itself make the name visible to unqualified lookup (3.4.1) or qualified lookup (3.4.3). [ Note: The name of the friend will be visible in its namespace if a matching declaration is provided at namespace scope (either before or after the class definition granting friendship). —end note ] If a friend function or function template is called, its name may be found by the name lookup that considers functions from namespaces and classes associated with the types of the function arguments (3.4.2). If the name in a friend declaration is neither qualified nor a template-id and the declaration is a function or an elaborated-type-specifier, the lookup to determine whether the entity has been previously declared shall not consider any scopes outside the innermost enclosing namespace. [ Note: The other forms of friend declarations cannot declare a new member of the innermost enclosing namespace and thus follow the usual lookup rules. —end note ]
我想提一下,将函数名括在括号中会抑制参数相关查找。
对于这段代码
int main()
{
A::X x;
( f )(x);
}
将找不到函数 f。
关于c++ - 哪个命名空间包含这个友元函数的声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25692402/
在 C++ 中,我使用多态类和友元来创建一个基本的“ friend 组”。但是,当我尝试访问类人(男孩类的 friend )的私有(private)年龄函数时,我无法访问它。有什么问题吗? /* Po
当一个 lambda 函数在 C 类的友元函数 F 中声明时,该 lambda 函数是否可以访问 C私有(private)成员(member)?具体来说,标准允许吗? 最佳答案 C++11 §[exp
首先回顾以前所学的构造函数 类的构造函数用于对象的初始化 构造函数与类同名并且没有返回值 构造函数在定义时被自动调用 由于构造函数没有返回值不能判断执行结果,所以不能保证初始
我想要一个模板化的友元函数。但是,我不知道如何使它在没有模板化功能的情况下以相同的方式工作。这是一个示例代码 #include namespace ns{ struct Obj { frie
我目前正在创建一个实用程序类,其中将包含重载的运算符。使它们成为成员或非成员 (friend) 函数的优缺点是什么?还是根本不重要?也许对此有最佳实践? 最佳答案 我会选择“C++ 编码标准:101
template struct test { template friend struct test; }; int main() {} 这是完全有效的代码,不是吗?我问是因为 MSVC+
相对于Java而言,友元是C++中特有的一种元素,很多教材上对其介绍的相对较少,因此初学的时候往往不能很快掌握,本文总结了友元的用法和一些注意的地方,供大家参考借鉴。希望能对初学C++的朋友起到一点
类中声明的友元函数和公共(public)函数有什么区别?当我们可以将好友功能公开并具有相同的访问权限时,使用好友功能还有什么意义呢? 最佳答案 public 指类成员函数的访问级别。 成员函数可以是公
如何在所有派生类中没有 friend 的情况下访问 privateMember? class parent{...}; //a virtual class class A: public paren
我是初学者级别的面向对象编程爱好者。我遇到了以下难题: class A { }; class B { protected: friend class A; }; class C { publi
前言:这些知识点属于C++较为前期的内容,博主在今年刷笔试题的时候遇到多次,所以特地这这篇博客再复习了一下。 1.构造函数的初始化 在推导之前,关于初始化我们先达成一点共识:初始化只能一次(记住这点)
大家好, 我正在尝试为我的数学 vector 类重载加法运算符。我的(看似逻辑正确的)简化代码是: template class Vector2 { private: T m_da
我是一名优秀的程序员,十分优秀!