草庐IT

this_call

全部标签

c++ - 异常 : bad_weak_ptr while shared_from_this

当我这样做时出现异常:std::bad_weak_ptr->shared_from_this()templateclasspainter_record_t{.......private:std::shared_ptr_owner;}这里我想在构造函数中设置“问题”对象:templateclassstream_record_t:publicpainter_record_t{public:stream_record_t(std::shared_ptrowner):painter_record_t(owner){//...}}我有基类:classi_painter_t{public:virt

c++ - 在 gmock 的 EXPECT_CALL 中调用 sleep()

我试图在调用FuncHelper之前在.WillOnce中做一些sleep。所以我需要类似于以下内容的内容:EXPECT_CALL(*_mock,Func(_,_,_)).Times(1).WillOnce(DoAll(InvokeWithoutArgs(sleep(TimeToSleep)),Invoke(_mock,&M_MyMock::FuncHelper)));是否可以在.DoAll中使用arg调用sleep()?C++98是首选。更新:该解决方案基于@Smeeheey的回答并使用C++98。templatevoidSleep(){sleep(N);}...EXPECT_CAL

c++ - 返回 *this 和 this 之间的区别 - C++

ClassA{//Code...A&operator++(){//code..return____;}Aoperator++(){//code..return___;}我什么时候应该返回*this或this?我理解this是一个指针,*this是指针的取消引用,但是当函数需要通过引用或值来获取值时,我无法决定返回什么。 最佳答案 this在您的函数中属于A*类型,因此返回该类型不合适。*this在您的函数中属于A&类型,它可以绑定(bind)到A&或A。当重载前缀++运算符时,返回A&是函数的正常返回类型。

C++ 模板和 "no matching function to call"

我遇到了一个奇怪的错误。我有以下签名的功能:templatestaticboolConvertCbYCrYToRGB(constCharacteristicspace,constDATA*input,DATA*output,constintpixels){后来这样称呼:casekByte:returnConvertCbYCrYToRGB(space,(constU8*)input,(U8*)output,pixels);casekWord:returnConvertCbYCrYToRGB(space,(constU16*)input,(U16*)output,pixels);casek

c++ - type_info 不考虑 cv 限定符 : is this right?

这段代码打印1是正确的行为还是g++4.5的怪癖?#include#includeusingnamespacestd;intmain(){structA{};cout我认为cv限定符的不同类型作为非常不同的类型受到威胁,即使较少的cv限定类型可以隐式转换为更多cv限定的类型。 最佳答案 typeid根据C++标准(摘自ISO/IEC14882:2003的§5.2.8)忽略cv限定符:Thetop-levelcv-qualifiersofthelvalueexpressionorthetype-idthatistheoperandof

c++ - free.c 抛出异常 "this program has stopped working"

当我使用VisualC++2010Express的调试器运行程序(server.exe)时,它运行完美,但是当我将它作为exe运行时它却没有;它崩溃并显示“Server.exe已停止工作”对话框。接下来我将exe重命名为“ServerInstaller.exe”并且它工作了,所以我认为这是一个权限错误,但它不适用于管理员模式下的“Server.exe”。然后我将VC++中的调试器附加到“Server.exe”程序,它在“free.c”中出现异常。这个文件中的代码是void__cdecl_free_base(void*pBlock){intretval=0;if(pBlock==NULL

this 的 C++ 类型

我正在用C++编写一些模板代码,我发现如果我可以确定this的类型,它会使代码更短/更好/更有用。我不想使用C++0x,因为代码要向后兼容旧的编译器。我也不想使用BOOST。我所拥有的是这样的:structMyLoop{templatevoidRun(intiterations,Contextc){MyUtility::templateWrapLoop(iterations,c);}};这可以用于一些有趣的循环优化。我不喜欢在MyUtility模板特化中使用MyLoop。使用C++0x,可以使用如下内容:structMyLoop{templatevoidRun(intiteration

c++ - 谷歌模拟 : why NiceMock does not ignore unexpected calls?

我正在使用GoogleMock1.7.0和GoogleTest1.7.0。问题是当我使用NiceMock时,由于意外的模拟函数调用(根据GoogleMock文档,NiceMock应该忽略它)导致测试失败。代码如下所示://GoogleMocktest#include#includeusing::testing::Return;using::testing::_;classTestMock{public:TestMock(){ON_CALL(*this,command(_)).WillByDefault(Return("-ERRNotUnderstood\r\n"));ON_CALL(*

c++ - 使用尽可能少的代码将非静态方法包装到带有 "this"参数绑定(bind)的 std::function

这是我正在尝试做的事情:templateclassCSignal{public:voidconnect(std::functiontarget){m_connections.emplace_back(target);}private:mutablestd::vector>m_connections;};connect非常适合静态方法或全局函数。现在,如果我想传递一个成员方法怎么办?看来这是我唯一的选择:structMyStruct{voidprint(floata,intb){std::cout如果我不必指定非常麻烦的占位符,它会适合我。所以我尝试另一种方法。我为成员方法添加了一个新的

c++ - 为什么 std::shared_ptr 的别名构造函数不初始化 std::enabled_shared_from_this?

考虑以下代码:structFoo:std::enable_shared_from_this{};structBar{Foofoo;};intmain(){std::shared_ptrbar_p(newBar);//makeshared_ptrtomemberwithaliasingconstructorstd::shared_ptrfoo_p(bar_p,&bar_p->foo);assert(bar_p->foo.shared_from_this());//fail!throwsbad_weak_ptr}不幸的是,它没有按预期工作(至少在GCC4.8.2中)。我查看了代码,似乎别名