草庐IT

FHE-friendly

全部标签

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++成为 friend

假设我有两个类(class)Widget^|Window我还有另一个类应用程序:定义如下classApplication{public:...private:friendWidget;};这不会让Window访问Applicationsprotected和private成员。有没有一种方法可以在不将Window和任何后续“Widget”声明为Application的友元的情况下完成此操作? 最佳答案 不,这是不可能的。friendship不可继承。此外,friendship表示两个实体之间有意强耦合因此,如果您的设计确实需要如此强的

c++ - 白盒测试—— friend 还是预处理器?

假设我们有这样一个类:classTestee{public:voidFunc()private:voidauxFunc()};我们想对其进行白盒单元测试。您认为哪种方法更好?将测试类声明为测试类的friend?或者像这样使用预处理器:classTestee{public:voidFunc()#ifndefUNITTEST_SYMBOLprivate:#elifpublic:#endifvoidauxFunc()};稍后在测试文件中#defineUNITTEST_SYMBOL#include"Testee.h"#undefUNITTEST_SYMBOL那么,您认为哪种方法更好?或者,也许

c++ - 在 friend 的类中包含头文件

我想知道您是否必须在使用它作为友元的类中#include"Class1.h"。例如,授予Class1类权限的类的.h文件。classClass2{friendclassClass1;}您需要#include"Class1.h"还是没有必要?同样在Class2类中,永远不会创建或使用Class1对象。Class1只是操纵Class2而不是相反。 最佳答案 语法是:friendclassClass1;不,您不包含header。更一般地说,除非您实际上以某种方式使用类定义(例如,您使用该类的实例并且编译器需要知道其中的内容),否则您不需要

c++ - 运算符的 friend 特定模板实例化

我有一个类模板和一个需要访问其私有(private)字段的运算符模板。我可以交一个模板friend:templateclassA{intx;templatefriendbooloperator==(constA&a,constA&b);};templatebooloperator==(constA&a,constA&b){returna.x==b.x;}intmain(){Ax,y;x==y;return0;}但是有没有可能只制作operator==friendA而不是制作operator==A的friend? 最佳答案 如果fri

c++ - friend 类的部分模板特化?

我有一个简单的类X和一组模板化类Y。我希望第一个模板化参数恰好是X的所有类Y都成为X本身的friend。下面希望传达我想要的,但是friend语句给出了编译错误。templateclassY{};classX{public:X(intvalue):i(value){}constint&getI()const{returni;}private:inti;templatefriendclassY;};我不确定完全允许友元语句模板化(更不用说友元语句的部分模板化了)。有没有办法做到这一点?还是我无法一一列出所有friend?谢谢,马特 最佳答案

c++ - 限制类的模板 friend

考虑以下代码:#includeclassS{staticconstinti=42;templatefriendvoidf();};templatevoidf(){std::cout();f();}我在这里只想允许访问类的私有(private)部分S至f,但不适用于f.IE。我想得到类似'i'isaprivatememberof'S'的编译器错误对于f()行。如何实现? 最佳答案 模板实例化是一个函数,所以命名就可以了:voidf().不过,您需要事先声明:[C++03:11.4/9|C++11/C++14:11.3/11]:Ifaf

C++ : friend declaration ‘declares a non-template function

我在重载时遇到问题流运算符(operator),我找不到解决方案:templateclassNVector{inlinefriendstd::ostream&operator&rhs);};templateinlinestd::ostream&NVector::operator&rhs){/*SOMETHING*/returnlhs;};它产生以下错误信息:warning:frienddeclaration‘std::ostream&operatorerror:‘std::ostream&NVector::operator如何解决这个问题?非常感谢。 最佳答

c++ - 如何解决 "class must be used when declaring a friend"错误?

classtwo;classone{inta;public:one(){a=8;}friendtwo;};classtwo{public:two(){}two(onei){cout我从Dev-C++收到此错误:aclass-keymustbeusedwhendeclaringafriend但是用MicrosoftVisualC++编译器编译时它运行良好。 最佳答案 你需要friendclasstwo;代替friendtwo;此外,您不需要单独转发声明您的类,因为友元声明本身就是一个声明。你甚至可以这样做://noforward-de