今天我对朋友的功能有疑问.两个班可以有相同的朋友功能吗?说出friend void f1();
在A类和B类中声明的例子
.这可能吗?如果是这样,函数f1()可以访问两个类的成员吗?
考虑以下:
namespace N {
struct A { };
struct B {
B() { }
B(A const&) { }
friend void f(B const& ) { }
};
}
int main() {
f(N::B{}); // ok
f(N::A{}); // error
}
Run Code Online (Sandbox Code Playgroud)
在第一种情况下,案例成功 - 我们考虑相关的名称空间N::B和查找N::f(B const&).大.
第二种情况失败了.为什么?根据[namespace.memdef]:
如果
friend非本地类中的声明首先声明了类,函数,类模板或函数模板,则该友元是最内层封闭命名空间的成员.[...]如果调用了友元函数或函数模板,则可以通过名称查找找到其名称,该名称查找考虑名称空间中的函数和与函数参数类型相关联的类(3.4.2).
的关联的命名空间N::A是N,它的f一个成员,所以为什么不通过查找发现的?
c++ friend-function language-lawyer argument-dependent-lookup
以前我学会了用C++重载Operators作为成员函数以及类的友元函数.虽然,我知道如何使用这两种技术在C++中重载运算符.但我仍然感到困惑,**哪一个更好**?成员函数或朋友函数重载运算符,我应该使用哪个,为什么?请指导我!您的回复将受到极大的赞赏.我很高兴并感谢你的回答.
考虑以下代码:
template <int N>
struct X
{
friend void f(X *) {}
};
int main()
{
f((X<0> *)0); // Error?
}
Run Code Online (Sandbox Code Playgroud)
编译器似乎非常不同意.(MSVC08/10表示否,GCC <4.5表示是,但4.5表示否,sun 5.1表示是,intel 11.1表示也是,但是comeau说不(两者都是EDG)).
根据"C++模板 - 完整指南":
...假设一个涉及查找关联类中的朋友的调用实际上导致该类被实例化......虽然这是C++标准编写人员的明确意图,但标准中没有明确规定.
我找不到标准中的相关部分.任何参考?
考虑这种变化:
template <int N>
struct X
{
template <int M>
friend void f(X<M> *) {}
};
template <>
struct X<0>
{
};
int main()
{
X<1>();
f((X<0> *)0); // Error?
}
Run Code Online (Sandbox Code Playgroud)
这里的关键问题是注入的可行功能是否X<1>应该在ADL期间可见X<0>?它们是否相关?上面提到的所有编译器都接受此代码,但Comeau仅在宽松模式下接受它.不确定标准对此有何看法.
你对此有何看法?
来自http://www.learncpp.com/cpp-tutorial/142-function-template-instances/
class Cents
{
private:
int m_nCents;
public:
Cents(int nCents)
: m_nCents(nCents)
{
}
friend bool operator>(Cents &c1, Cents&c2) // <--- why friend?
{
return (c1.m_nCents > c2.m_nCents) ? true: false;
}
};
Run Code Online (Sandbox Code Playgroud)
我们也可以像这样实现它:
class Cents
{
private:
int m_nCents;
public:
Cents(int nCents)
: m_nCents(nCents)
{
}
bool operator> (Cents& c2) // <---
{
return (this->m_nCents > c2.m_nCents) ? true: false;
}
};
Run Code Online (Sandbox Code Playgroud)
使用第二个实现有任何缺点吗?
在Bjarne Stroustrup撰写的The C++ Programming Language一书中,作者介绍了一个必须实现函数inv()的类Matrix.在第11.5.1节中,他谈到了这样做的两种可能性.一个是建立一个成员函数,另一个是使朋友函数inv().然后在第11.5.2节结束时,他谈到选择是否使用朋友或成员函数,他说:
如果inv()确实反转了Matrix m本身,而不是返回一个与m相反的新Matrix,那么它应该是一个成员.
为什么会这样?朋友函数不能改变Matrix的状态并返回对该矩阵的引用吗?是因为在调用函数时传递临时矩阵的可能性是什么?
我有重载operator <<为模板类的问题.我使用的是Visual Studio 2010,这是我的代码.
#ifndef _FINITEFIELD
#define _FINITEFIELD
#include<iostream>
namespace Polyff{
template <class T, T& n> class FiniteField;
template <class T, T& n> std::ostream& operator<< (std::ostream&, const FiniteField<T,n>&);
template <class T, T& n> class FiniteField {
public:
//some other functions
private:
friend std::ostream& operator<< <T,n>(std::ostream& out, const FiniteField<T,n>& obj);
T _val;
};
template <class T, T& n>
std::ostream& operator<< (std::ostream& out, const FiniteField<T,n>& f) {
return out<<f._val;
}
//some other definitions
}
#endif
Run Code Online (Sandbox Code Playgroud)
在主要我只是
#include"FiniteField.h"
#include"Integer.h"
#include<iostream>
using std::cout;
using …Run Code Online (Sandbox Code Playgroud) c++ templates operator-overloading visual-studio-2010 friend-function
我想知道我的目标是否可行.
我有一个类Class
#include<iostream>
template<class T> class Class;
template<class T, class W> Class<W> f(Class<T>& C, const Class<T>& D);
template<class T> class Class {
protected: // this could be private
T m_t;
public:
Class(): m_t(T()) {}
Class(T t): m_t(t) {}
T& getT() { return m_t; }
template<class U, class W> friend Class<W> f(Class<U>& C, const Class<U>& D);
};
template<class T, class W> Class<W> f(Class<T>& C, const Class<T>& D)
{
C.m_t += D.m_t;
Class<W> R;
std::cout << R.m_t << std::endl; // I don't …Run Code Online (Sandbox Code Playgroud) 我知道以下 C++ 代码片段应该在 的定义中产生错误g,因为它p.t.x是私有的,无法在那里访问。
class P {
class T {
int x;
friend class P;
};
T t;
friend void g(P &p);
};
void g(P &p) { p.t.x = 42; }
Run Code Online (Sandbox Code Playgroud)
令我困惑的是下一个片段。不同之处仅在于朋友函数的定义g现在出现在class 中P。
class P {
class T {
int x;
friend class P;
};
T t;
friend void g(P &p) { p.t.x = 42; }
};
Run Code Online (Sandbox Code Playgroud)
Clang++(6.0.0-1ubuntu2 和 Apple 版本 clang-1100.0.33.8)编译后者没有错误,而 GNU C++(7.5.0-3ubuntu1~18.04)产生与前一个片段相同的错误。
我知道g后一种情况中定义的函数与前一种情况中定义的函数不在同一范围内(参见相关问题和 …
我一直在使用以下使用 GCC 编译的程序,但被 Clang 和 MSVC 拒绝。
template <typename>
class X {
void f(){}
};
class Y
{
friend void X<int>::f();
};
int main()
{
X<int> t;
t.f();
Y b;
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,上面的程序适用于 GCC,但 Clang 说:
<source>:9:25: error: friend function 'f' is a private member of 'X<int>'
friend void X<int>::f();
~~~~~~~~^
<source>:9:25: note: implicitly declared private here
<source>:15:7: error: 'f' is a private member of 'X<int>'
t.f();
^
<source>:9:25: note: implicitly declared private here
friend void X<int>::f();
^
2 …Run Code Online (Sandbox Code Playgroud)