jjzjj

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

c++ - 在模板类中编写友元函数声明的正确方法是什么?

我正在尝试编写自己的vector模板类,但在编写友元函数声明时遇到了一些问题。一开始我是这样写的:template>classvector{public:friendbooloperator==(constvector&,constvector&);};但是编译器报了一个警告,说我声明了一个非模板函数。所以我将好友声明更改为:template>classvector{public:templatefriendbooloperator==(constvector&,constvector&);};到目前为止一切都很好,但我认为仍然存在问题。如果我那样写,我就把所有operator==将两

c++ - 这是对类(Class)友情的恰当运用吗?

在创建Windows父类和子类对话框时,让子类成为父类的友元以访问其私有(private)数据通常是个好主意还是应该使用访问函数? 最佳答案 很少需要friend-通常是当您需要在一个类中重新实现一些深层行为而不重写它以便它们都从单个基类继承或不提供大量访问者时。只有一次我需要它是在ActiveX中重写一个基于openGL的渲染器——当我需要获取大量低级模型数据,但不能(出于非技术原因)重新实现一个通用的ABC时。 关于c++-这是对类(Class)友情的恰当运用吗?,我们在StackO

c++ - 如何授予函数模板好友访问类的权限?

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Howtoallowtemplatefunctiontohavefriend(-like)access?如何让函数模板Loadfriend访问类Foo?这里的目标是限制对构造函数的访问:只有函数模板Load可以构造。CODE(请忽略内存泄漏)classFoo{Foo(){}templatefriendFooconst&Load();//errorhere};templateTconst&Load(){return*(newT);}intmain(intargc,char*argv[]){Fooconst&f=

c++ - 如何初始化一个私有(private)嵌套类类型的静态字段?

Outer.hpp:classOuter{classInner{Inner(){}};staticInnerinner;}Outer.cpp(在顶层,例如不在函数体内):Outer::InnerOuter::inner;我收到以下错误:errorC2248:'Outer::Inner::inner':cannotaccessprivatememberdeclaredinclass'Outer::Inner'我没有使用完全符合C++11(VisualStudio2010)的编译器,因此无法在声明时定义字段。 最佳答案 诀窍是让Oute

c++ - 专门为私有(private)类(class)的功能?

有没有办法为私有(private)类专门化一个函数(比如,std::swap)?例如,当我测试这个时:#includeclassOuter{structInner{inta;voidswap(Inner&other){usingstd::swap;swap(this->a,other.a);}};public:staticvoidtest();};namespacestd{templatevoidswap(Outer::Inner&a,Outer::Inner&b){a.swap(b);}}voidOuter::test(){usingstd::swap;Innera,b;swap(a

c++ - 哪个命名空间包含这个友元函数的声明?

下面的friend函数不是通过普通查找(§7.3.1.2/3)找到的,而是通过ADL找到的(§3.4.2/4第二个要点),因此代码编译通过并正常执行(liveexample)。但是函数f没有在任何命名空间中声明。例如,如果您尝试将调用f(x);替换为这些调用中的任何一个::f(x);,A::f(x);或A::X::f(x);,代码将无法编译。哪个命名空间包含这个友元函数的声明?标准对此有何规定?#includenamespaceA{classX{inti;friendvoidf(Xx){std::cout 最佳答案 来自C++标准1

c++ - 纯虚拟 friend 类

我有课A有一个指向纯虚类实例的指针B.类C源自B并且会自动有一个指向A的指针(它是它的父级),并且需要访问它的成员。这可以通过添加friendclassC来实现内部类A,虽然这对于将从B派生的每个类都是必需的.代码示例:classA{public:friendclassB;//ThisdoesnotallowderivedclassestobefriendsfriendclassC;//NowderivedclassBhasaccessto`DoDomething`,butthenthisisneededforeverysinglederivedclassprivate:voidDoD

c++ - 如何将模板函数声明为模板嵌套类的 friend ?

我怎样才能制作get封闭范围内的一个函数,可以访问outer::inner的私有(private)构造函数?templatestructouter{templateclassinner{inner(){}public:friendinnerget(outer&){return{};}};};intmain(){outerfoo;outer::innerbar=get(foo);}我尝试通过制作inner来宣布它不在类里面有一个templatefriendinnerget(outer&);但这也不起作用。 最佳答案 Ihavetrie

c++ - C++ 中的本地类

我正在阅读Balagurusamy(http://highered.mcgraw-hill.com/sites/0070593620/information_center_view0/)的C++面向对象编程中的“本地类”概念。最后一行说“封闭函数不能访问本地类的私有(private)成员。但是,我们可以通过将封闭函数声明为友元来实现这一点。”现在我想知道突出显示的部分如何完成?这是我正在尝试但没有成功的代码,#includeusingnamespacestd;classabc;intpqr(abct){classabc{intx;public:intxyz(){returnx=4;}f