草庐IT

Friend-ing

全部标签

c++ - Friend 类或 Friend 成员函数 - 前向声明和 Header 包含

是的,这个问题话题已经讨论了很多次了。我几乎清楚其中的区别。我对书中的一个例子只有一个疑问。这个问题与mypreviousquestion有关,我在C++Primer一书中介绍了2个类作为示例。在引用那些类时,本书引用了以下段落,特别涉及将WindowManager类的成员函数声明为友元函数。内容如下:Makingamemberfunctionafriendrequirescarefulstructuringofourprogramstoaccommodateinterdependenciesamongthedeclarationsanddefinitions.Inthisexampl

c++ - 如何为 friend 函数中定义的类授予友元?

这个问题是不言自明的,但如果需要的话,这里有一个例子:假设我有一个带有私有(private)构造函数的类“Thing”,它是一个函数“make_thing”的friend:classThing{friendstd::shared_ptrmake_thing();Thing(){std::cout“make_thing”函数内部定义了一个结构:std::shared_ptrmake_thing(){//err:Thing::Thing()isprivatewithinmake_shared//autothing=std::make_shared();//thisisokaystructA

c++ - 运算符在类中定义为 friend 的奇怪行为

我不明白下面这段代码是怎么回事:structA{};structB{B(){}B(constA&){}friendBoperator*(constB&,constB&){returnB();}};intmain(){Bx=A()*A();return0;}当我编译(同时使用clang和gcc4.9.2)时,我在“Bx=A()*A()”行收到一条错误消息;clang说“二进制表达式的操作数无效”。如果我从类内部获取operator*定义,一切都100%ok!structA{};structB{B(){}B(constA&){}friendBoperator*(constB&,constB

c++ - dlopen'ing 时是否运行静态初始化(和/或其他)代码?

当您dlopen()一个共享对象时,是否有一种机制可以让该DLL中的代码在不显式调用的情况下执行?具体来说,dlopen()的调用者可能不知道的全局变量/静态变量的C++静态初始化代码?我很确定答案应该是"is",但我不记得是什么机制使它发生,以及如何利用它来运行任意代码。 最佳答案 是的:dlopen遵循在加载时运行代码的ELF二进制格式机制。实际上有两种这样的机制:旧版本使用特殊的.init和.fini部分,其中包含用于dlopen和的函数指针数组dlclose调用。由于这些部分在运行时可能不存在,因此还有指向相应部分的DT_I

c++ - Friend 模板函数(在非模板类中),C++

如果我有一个非模板(即“普通”)类并希望有一个模板友元函数,我该如何编写它而不导致编译器错误?这是一个示例来说明我正在尝试做的事情:templatevoidbar(T*ptr);classMyClass//notethatthisisn'tatemplateclass{private:voidfoo();templatefriendvoidbar(T*);//ERROR:compilergivesmeallkindsofgrief};templatevoidbar(T*ptr){if(ptr){MyClassobj;obj.foo();}}我使用的是VisualStudio2005,我

c++ - friend ,模板,重载<<

我正在尝试使用友元函数重载Point.cpp:11:error:shadowstemplateparm'classT'Point.cpp:12:error:declarationof'constPoint&T'对于这个文件#include"Point.h"templatePoint::Point():xCoordinate(0),yCoordinate(0){}templatePoint::Point(TxCoordinate,TyCoordinate):xCoordinate(xCoordinate),yCoordinate(yCoordinate){}templatestd::os

c++ - MSVC9.0 bug 或对虚拟继承的误解和 friend ?

考虑以下代码:classA{friendclassB;friendclassC;};classB:virtualprivateA{};classC:privateB{};intmain(){Cx;//OKdefaultconstructorgeneratedbycompilerCy=x;//compilererror:copy-constructorunavailableinCy=x;//compilererror:assignmentoperatorunavailableinC}MSVC9.0(VisualStudio2008的C++编译器)确实会生成默认构造函数,但无法为C生成复制

c++ - 最烦人的 friend ?友化专门的自由函数模板会引发编译错误(重载方法时)

代码我将问题简化为这个例子(粘贴为一个block以便于编译)///\briefThefree-functiontemplate,///whichisoverloadingamethodwiththesamenameinAbstractAbelow.templateinlineconstToverloadedMethod(constT&lhs,constT&rhs){returnT(lhs.value+rhs.value);}///\briefAbstractAclassclassAbstractA{public:AbstractA(intaVal):value(aVal){}inlin

c++ - 制作成员函数,一个类的 friend

我一直在尝试编写代码来实现一个成员函数,该成员函数可以通过将其声明为类中的友元来访问类的私有(private)数据。但是我的代码失败了,我似乎无法弄清楚它有什么问题:#includeusingnamespacestd;classA;classB{private:intb;//ThisistobeaccessedbymemberfunctionofApublic:friendvoidA::accessB();};classA{private:inta;public:voidaccessB();};voidA::accessB(){By;y.b=100;cout我正在尝试使用getAcce

c++ - 如何与 "template using"定义的模板(别名)类成为 friend ?

类B想和每个人成为friendC.我正在努力寻找解决方法。只要我不添加有问题的行,下面是成功编译的完整代码。#includeusingnamespacestd;enumEN{EN1,EN2};templateclassC{public:C(){std::coutclassB{templateusingCT=C;//templatefriendclassCT;//ct;}};intmain(){B::test();return0;}这是我尝试过的(全部失败):-templatefriendclassC;templatefriendclassCT;templatefriendclassCT