草庐IT

C++友元类

全部标签

c++ 实现友元/内联函数

我似乎找不到这个新手问题的答案。如果我有课//头文件(.h)ClassX{public:friendbooloperator==(constX&,constX&);inlinesize_typerows()const;};等等...当我去实现X的.cpp文件时,我是否应该在.cpp文件的函数名中包含inline&friend这两个词。即,我是否应该像下面这样实现我的文件//CPPfile(.cpp)#include"X.h"friendbooloperator==(constX&,constX&){//implementationgoeshere//returntrue/false}i

c++ - 将一个未定义的类作为友元,然后再定义它

交一个陌生的friendtemplateclassList{protected:classa{intx;inty;private:friendclassb;//classb{//Ifthatisnotaerrorthisshouldbeanerrorintz;Uy;};public:List(){a*ptr=(a*)newunsignedchar[sizeof(a)];}};intmain(){Listmylist;}请通过此链接,我将我的问题作为代码中的注释。我正在努力让另一个类(class)成为我类(class)的friend。但是交friend的时候不知道那个类(class)。允

c++ - 关于友元函数定义和命名空间范围

我正在读这个blogpostsection,然后我尝试使用提供的代码段。namespaceN{//2classA{friendvoidf(A){}//1};}如果我没理解错的话,//1中的定义会在//2所在的位置注入(inject)名称f。但是,它只能通过依赖于参数的查找获得。很好。帖子中有一句话引起了我的注意:7.3.1.2/3Namespacememberdefinitions[namespace.memdef]p3在命名空间中首次声明的每个名称都是该命名空间的成员。如果非本地类中的友元声明首先声明了类、函数、类模板或函数模板,则友元是最内层封闭命名空间的成员。friend声明本身

c++ - 模板对象的模板友元函数和命名空间

在以下C++示例代码中,GCC6和Clang3.8对正确的行为是什么存在分歧:这个人为的例子“有效”——如test()函数返回o.p在海湾合作委员会。在clang中,它调用(未定义的)函数get:templateclassobj{boolp=false;templatefriendTget(constobj&o){returno.p;}};templateTget(constobj&o);booltest(constobj&a){returnget(a);}将相同的代码放入命名空间会导致GCC执行与clang相同的操作。namespacens{templateclassobj{bool

c++ - 模板特化的友元声明失败

以下包含友元声明的代码因指示错误而失败(请参阅http://ideone.com/Kq5dy):templatevoidfoo(){}templateclassA{voidfoo();friendvoidfoo();//error:variableorfield'foo'declaredvoid};intmain(){foo();}如果友元声明和成员函数声明的顺序颠倒,则代码编译没有问题(参见http://ideone.com/y3hiK):templatevoidfoo(){}templateclassA{friendvoidfoo();voidfoo();};intmain(){f

c++ - 如何从友元函数访问 protected 构造函数?

我创建了一个类,我想强制任何试图构建对象的人使用unique_ptr。为此,我考虑声明构造函数protected并使用返回unique_ptr的friend函数。所以这是我想做的一个例子:templateclassA{public:friendstd::unique_ptr>CreateA(intmyarg);protected:A(intmyarg){}};templatestd::unique_ptr>CreateA(intmyarg){//SinceIdeclaredCreateAasafriendIthoughtI//wouldbeabletodothatreturnstd::

c++ - 使用 GCC 编译模板友元错误,但不使用 clang

此代码使用clang3.7.1编译(没有诊断),但使用GCC5.3.0编译失败>(liveexample):#includetemplatestructA{voidfoo(){static_cast(this)->implementation();}};structCrtp:A{templatefriendstructA;private:voidimplementation(){std::coutGCC的错误信息如下:main.cpp:13:16:error:specializationof'A'afterinstantiationfriendstructA;哪个是正确的,为什么?是G

c++ - 模板类友元运算符成员函数

我试图让一个模板化类中的友元函数进行编译,但我不明白错误消息和警告。我已经对这个问题进行了演示。我得到的错误是:prog.cpp:8:57:error:non-class,non-variablepartialspecializationCoperator+(constB&lhs,constC&rhs);prog.cpp:15:59:warning:frienddeclaration'Coperator+(constB&,constC&)'declaresanon-templatefunction[-Wnon-template-friend]friendCoperator+(const

c++ - 如何将可变参数模板函数声明为友元?

如何将可变参数模板函数声明为友元?例如如下:templateclassA{friend???MakeA???;//Whatshouldbeplacedhere???A(T){}};templateAMakeA(Args&&...args){Tt(std::forward(args));returnA(t);} 最佳答案 这很简单。它只是一个带有添加的friend说明符的模板声明:templateclassA{templatefriendAMakeA(Args&&...args);A(T){}};

c++ - 递归友元类

有没有办法解决这个问题:classB;classC{public:C(){}private:inti;friendB::B();};classB{public:B(){}private:inti;friendC::C();};给出错误:prog.cpp:8:error:invaliduseofincompletetype‘structB’prog.cpp:1:error:forwarddeclarationof‘structB’ 最佳答案 你就是不能这样做。移除循环依赖。 关于c++-递