草庐IT

friend-class

全部标签

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++ - 如何在一个类(class)中正确管理2个不同类型的容器?

我有以下(有点伪)代码,它处理2种不同(但有点相似)类型的2个容器,我讨厌这些重复的添加和删除(以及我的真实代码中的2个搜索函数)classPureAbstractClass{public:virtualcharFunc()=0;}classPureOpt1:PureAbstract{public:virtualintFOption1(A,B,C)=0;//Notice'C'}classPureOpt2:PureAbstract{public:virtualintFOption2(A,B,D)=0;//Notice'D'}classHandler{public:voidAdd(Pure

C++ 销毁顺序 : Calling a field destructor before the class destructor

有没有办法在类析构函数之前调用字段析构函数?假设我有2个类Small和Big,Big包含一个Small的实例作为它的字段因此:classSmall{public:~Small(){std::cout当然,这会在小析构函数之前调用大析构函数:BigdestructorSmalldestructor我需要在Big析构函数之前调用Small析构函数,因为它会为Big析构函数执行一些必要的清理工作。我可以:显式调用small.~Small()析构函数。->但是,这会调用Small析构函数两次:一次显式调用,一次在Big析构函数执行后调用。有一个Small*作为字段并在Big析构函数中调用del

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++ - SSE 与类(class)保持一致

有一些非常奇怪的问题,作为c++的初学者,我不知道为什么。structDeviceSettings{public:....somevariablesDXSizeBackbufferSize;....somemethods};structDXPoint;typedefDXPointDXSize;__declspec(align(16))structDXPoint{public:union{struct{intx;inty;};struct{intwidth;intheight;};intdataint[2];__m128im;};DXPoint(void);DXPoint(intx,in

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++ - gdb python编程: how to write code that will set breakpoints to every method of a C++ class?

我希望能够在gdb中为C++类的每个方法设置断点。我认为最简单的方法可能是python,因为现在python可以完全访问gdb。我对python知之甚少,而在它上面加上gdb,它就更难了。我想知道是否有人知道如何编写一个类python代码来为gdb中命名类的每个方法设置断点。 最佳答案 假设您使用调试符号进行编译,您甚至不需要python:rbreaksource.cpp:. 关于c++-gdbpython编程:howtowritecodethatwillsetbreakpointsto

c++ - 错误 : ‘Class’ was not declared in this scope

请问,如何在另一个类中定义类。在下面的代码中。我尝试以#define"CCompField.h"的方式定义它,但它不起作用。:(。我认为这是非常常见的编程问题,可能在互联网上已经解决了100000次,但我不知道如何找到它。感谢您的帮助。#ifndefCNEWGAME_H#defineCNEWGAME_HclassCNewGame{public:CNewGame();~CNewGame();voidBeginnerGame();voidIntermediateGame();voidAdviceGame();voidHowToPlay();voidNetGame(intmode);intM

c++ - C++ 中两个具有友元方法的类

目前我正在阅读一本关于C++的书,里面有一些练习。其中一个练习要求构建两个类,每个类都有一个友元方法。我目前的猜测是这样的:#includeusingstd::cout;usingstd::endl;classY;classX{public:voidfriendY::f(X*x);voidg(Y*y){cout但我的猜测没有编译,因为类X有voidfriendY::f(X*x);方法声明。我该如何解决这个难题?请再给我一些猜测。 最佳答案 为了将函数声明为友元,编译器必须首先看到它,而C++不允许成员函数的前向声明。因此,您尝试做的