草庐IT

this_call

全部标签

C++ this 和常量对象

你能告诉我为什么这段代码有效吗?replace_if算法使用了重载的operator()。在main函数中,我创建了IsEqual类的常量对象,因此只应使用常量函数成员。不知何故,恒定性不起作用,该运算符被调用。#include#include#includeclassIsEqual{intvalue;public:IsEqual(intv):value(v){}booloperator()(constint&elem){this->value=6;returnelem==value;}};intmain(){constIsEqualtst(2);std::vectorvec={3,2

C++ 使用 'this' 作为参数

我有一个大致像这样的类:classMeshClass{public:AnchorgetAnchorPoint(x,y){returnAnchor(this,x,y);}private:points[x*y];}我想制作另一个代表“anchor”的类,它可以访问网格并修改该点,如下所示:classAnchor{public:Anchor(&MeshClass,x,y)moveAnchor(x,y);}问题是当我尝试在MeshClass::getAnchorPoint方法中创建Anchor时,类似于returnAnchor(this,x,y)但是因为this是const我不能。作为一种解决

c++ - GMock - 使用 ON_CALL 为重载方法返回默认值

我正在尝试为包含三个重载方法的类编写模拟,即:#include#includeusing::testing::_;using::testing::Return;using::testing::A;using::testing::ByRef;using::testing::Ref;using::testing::TypedEq;structFoo{intfooMethod(constint&intParam){return0;}intfooMethod(constfloat&floatParam){return0;}intfooMethod(conststd::string&string

c++ - Qt 5.5 与 qmake : Linker cannot resolve OpenGL function calls

当使用Qt5.5、qmake和MSVC13编译带有一些基本OpenGL函数调用的基本样板Qt应用程序时,出现以下链接器错误:glwidget.obj:-1:error:LNK2019:unresolvedexternalsymbol__imp__glClear@4referencedinfunction"public:virtualvoid__thiscallGLWidget::initializeGL(void)"(?initializeGL@GLWidget@@UAEXXZ)glwidget.obj:-1:error:LNK2019:unresolvedexternalsymbol

c++ - 是否编译了带有 if(this==NULL) 测试的类函数?

我在我们的实验中看到了这段代码片段,它实际上是在MSVC2008和G++中编译的。voidLinkList::Insert(Tn){if(this==NULL)//somecodehere}据我所知,this不能为null,因为如果未实例化,则不能在C++中调用类函数。这是一个有效的代码吗?如果是这样,背后的原因是什么?它可以用在什么地方? 最佳答案 sinceyoucannotcallaclassfunctionsinc++ifitwasn'tinstantiated问题是,你可以,但它leadstoundefinedbehavi

c++ - 'giving out' 是构造函数中对 'this' 的引用吗?

我的代码:Scene::Scene(conststd::string&scene_file):ambient_light(0,0,0),background(0,0,0){scene_parserparser(*this);parser.parse(scene_file);}scene_parser是Scene的friend,在parse方法中它访问(r/w)Scene的成员。这会导致任何问题吗? 最佳答案 是的,给出对this的引用是可以的。但是,您通常希望在其他对象稍后使用该指针时执行此操作。您的用例看起来会在构造函数完成之前立

c++ - 这个私有(private)变量 "not declared in this scope"怎么样?

我目前正在尝试学习更多有关C++面向对象设计的知识(熟悉Java),但遇到了一些困难。我试图将这个项目放在一起,以在使用SFML构建图形和音频的游戏中学习这些原则。我有以下两个文件。WorldObject.h#ifndefWORLDOBJECT_H#defineWORLDOBJECT_H#include#include#include"ImageManager.h"classWorldObject{private:sf::Sprite_sprite;voidSetImagePath(std::stringpath);sf::SpriteGetGraphic();};#endif世界对象

c++ - 带有 "Do not show this again"复选框的 QMessageBox

如何在下方显示带有“不再显示”复选框的消息框?我想象的东西看起来像这样: 最佳答案 Qt5.2添加了将QCheckBox添加到QMessageBox的可能性。看看QMessageBox::setCheckbox这是一些演示代码if(this->showMsgBox){QCheckBox*cb=newQCheckBox("OkayIunderstand");QMessageBoxmsgbox;msgbox.setText("AmInerve-wrecking?");msgbox.setIcon(QMessageBox::Icon::Q

c++ - 错误 : pure virtual method called - terminate called without an active exception - Aborted

在我的A.h文件中:classA{private:unsignedshortPC;public:A():PC(0){}virtual~A(){}virtualvoidexecute(unsignedshortPC)=0;};在我的B.h文件中:classB:publicA{private:intstatus;boolexe;public:B:status(0),exe(false){}virtualB(){}voidexecute(unsignedshortPC);};在我的B.cpp文件中:#include#include"B.h"voidB::execute(unsignedsho

c++ - 构造函数内部 "this"的 dynamic_cast

这个问题与这个问题非常相似Whycan'tIdynamic_cast"sideways"duringmultipleinheritence?,除了强制转换确实有效-只是不在构造函数中。标题:classA{public:virtual~A(){}voidprintA();};classB{public:B();virtual~B(){}voidprintB();private:std::stringmessage_;};classC:publicA,publicB{public:C(){}virtual~C(){}};来源:voidA::printA(){cout(this);if(a)