草庐IT

this-reference

全部标签

C++ : friends of class and "this" pointer

我有一个小问题要问你:),我知道每个方法都“secret地”获取它们所在的某个类的“this”指针,但为什么“友元”函数不会发生这种情况?是因为它们不是类的方法吗?谁能解释一下整个机器,我对“这个”到底是如何工作的很感兴趣!提前致谢!:) 最佳答案 friend函数和类仅用于编译器检查的访问控制。friend函数只是标准函数,因此调用约定不会有任何差异。friend函数不是任何类的成员,因此没有传递this指针(与static成员函数一样)类的非static成员函数将得到一个隐藏this指针(根据ABI这通常是第一个参数),stat

c++ - 谷歌测试 Gtest.cc :812: error: 'gettimeofday' was not declared in this scope

我正在尝试通过命令行通过MinGW编译Google测试,但是当我尝试使用命令进行编译时gcc-ID:\gtest-ID:\gtest\include-ID:\gtest\include\gtestsrc\gtest_main.ccsrc\gtest-all.cc它抛出错误Gtest.cc:812:错误:'gettimeofday'未在此范围内声明我通过VisualStudio成功编译,所以我猜这不是代码的错误。还有其他人遇到过这个问题并且知道解决方案吗? 最佳答案 and是两个不同的包含

c++ - 在构造函数的初始化列表中使用 "this"对 Qt 特别危险吗?

我需要关于“这个”主题的可靠信息:classMyClass,publicQWidget{public:MyClass(QWidget*parent=NULL):QWidget(parent),mpAnotherWidget(newQWidget(this)){};private:QWidget*mpAnotherWidget;};当然,在构造器或初始化列表中调用虚函数不是一个好主意。问题是:这段代码可以吗mpAnotherWidget(newQWidget(this))导致未定义的行为?!如果是这样:为什么?如果可以,请引用您的消息来源!谢谢! 最佳答案

c++ - Lua 5.3 undefined reference

我正在尝试学习如何将lua嵌入到C程序中,但我不擅长阅读技术文档,而且我还没有找到任何当前教程。这是我的程序:#include#include#include#includevoidreport_errors(lua_State*,int);intmain(intargc,char**argv){for(intn=1;n编译器给出了luaL_newstate、luaL_openlibs、luaL_loadfilex、lua_pcallk和lua_close的undefinedreference错误。我在一台Windows计算机上使用Code::Blocks,我已将luainclude目

c++ - 错误 : 'object' was not declared in this scope

我是C++的新手,正在尝试制作大富翁游戏。不幸的是,它仍然显示两个类之间的声明错误。我已经尝试了所有方法,但真的不知道问题出在哪里。错误:“玩家”未在此范围内声明。引擎.h#ifndefENGINE_H#defineENGINE_H#include"Player.h"#includeusingnamespacestd;classEngine{public:Engine();//methodthatstartswithgame,takerandomnumberforgettingnumberofplayers,setplayerstovectorvoidplay();//methodwh

c++ - boost::interprocess : cout a string variable when iterating through a map that references an object from a struct

我正在使用boost::interprocess在进程之间共享对象。我有两个文件,一个生成结构对象并将该对象传递到具有int索引的映射中的“server.cpp”;和一个“client.cpp”文件,它检索内存数据并遍历数据,输出到控制台。结构看起来像这样:structmydatao{stringMY_STRING;intMY_INT;};和对象:mydatao;o.MY_STRING="hello";o.MY_INT=45;服务器和客户端都能正确编译。但是出于某种原因,如果我尝试访问客户端中的字符串而不是float或整数,客户端可执行文件会抛出段错误。例如下面的second.MY_I

c++ - 为什么 "this"指针用于调用派生成员函数?

在this上阅读关于虚函数的教程(与本例无关)链接,我找到了这段代码。classWeapon{public:voidfeatures(){coutWeapon::features();cout类Weapon派生自类Bomb,其中调用了Weapon的成员函数。为什么使用“this”指针调用函数Weapon::features()?这不是已经隐式给出了吗? 最佳答案 this是隐式给出的,是否显式编写通常是风格问题。在您的情况下,我会说它不会提高可读性。然而,在其他情况下,显式写入this是有意义的,甚至是必要的为了避免局部变量和数据成

c++ - 基类中的唯一指针禁止实例化,错误为 "attempting to reference a deleted function"

我将我的C++工具链从VisualStudio2013更新到VisualStudio2017/2019。现在我遇到了一些形式的编译错误:(13):errorC2280:'OfflineFixture::OfflineFixture(constOfflineFixture&)':attemptingtoreferenceadeletedfunction(8):note:compilerhasgenerated'OfflineFixture::OfflineFixture'here(8):note:'OfflineFixture::OfflineFixture(constOfflineFi

c++ - BOOST_FOREACH : What is the error on using this of a STL container?

有谁知道为什么以下会在VC9上产生错误?classElem;classElemVec:publicvector{public:voidfoo();};voidElemVec::foo(){BOOST_FOREACH(Elem&elem,*this){//Dosomethingwithelem}return;}我得到的错误是:errorC2355:'this':canonlybereferencedinsidenon-staticmemberfunctions我现在拥有的唯一(hack)解决方案是:voidElemVec::foo(){ElemVec*This=this;BOOST_FO

c++ - 链接器错误 : undefined reference c++

我曾尝试查看类似的问题,但无法轻易找到对我的问题有所帮助的问题。我用C++创建了一个项目,并在UNIX上编译、链接和运行它。我的具体问题是对我在单独文件中声明的方法的undefinedreference。在文件SharedCache.cpp中,我有以下方法:intSharedCache::replaceLine(ullongaddress){intevictPID=-1;intcacheSet=calcCacheSet(address);//randomuniformly-distributedvalueforcachelinefloatnumLines=static_cast(CAC