草庐IT

call_me_virtual

全部标签

c++ - "If you' 已经写了一个编译测试,你've written a call to main"

在程序中调用mainviolatesC++标准voidf(){main();//anendlessloopcallingmain?Nothat'snotallowed}intmain(){staticint=0;std::cout在lecture中ChandlerCarruth,大约在“22.40”说ifyou'vewrittenacompilertestyou'vewrittenacalltomain这有什么关系,或者如何克服标准不允许的事实? 最佳答案 这里的要点是,如果你编写编译器测试代码,你可能会想用一些不同的参数集测试调用

c++ - 构造函数中抛出的异常 : is the destructor called?

这个问题在这里已经有了答案:Whatdestructorsarerunwhentheconstructorthrowsanexception?(3个答案)关闭8年前。如果在对象的构造函数中抛出异常,那么是否会调用析构函数?还是未定义的行为?(这就是为什么我不愿意说出我的编译器做了什么。)structfoo(){foo(){throw"bar";}~foo(){/*amIcalled*/}};foof;

c++ - 在 Visual Studio 中调用 std::swap 时的 std::bad_function_call

我正在尝试将我的代码从Linux移植到Windows。但是,对于VisualStudio,我的代码因以下错误而崩溃:MicrosoftC++exception:std::bad_function_callatmemorylocation这是我的代码:#includeclassFoo{public:Foo(int):m_deleter{[](){}}{}Foo(constFoo&)=delete;Foo(Foo&&)=default;Foo&operator=(constFoo&)=delete;Foo&operator=(Foo&&)=default;~Foo(){m_deleter(

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++ - 通常方法上的 Woverloaded-virtual 警告

我很困惑为什么下面的代码会产生Woverloaded-virtual警告。classTestVirtual{public:TestVirtual();virtualvoidTestMethod(inti);};classDerivedTestVirtual:publicTestVirtual{public:voidTestMethod();};派生类具有不带参数的常用方法TestMethod-签名不同于基类的类似虚拟方法。那为什么编译器不能解决这种情况呢? 最佳答案 警告的原因是无参数版本从基类中隐藏了int版本。DerivedTe

C++ 重载 virtual = 运算符

这是我的问题的代码:classICommon{public:virtualICommon&operator=(constICommon&p)const=0;};classCSpecial:publicICommon{public:CSpecial&operator=(constCSpecial&cs)const{//customoperationsreturn*this;}};CSpecialobj;基本上:我希望接口(interface)ICommon强制其后代实现=运算符,但不希望在实现中有任何类型转换。编译器说“无法实例化抽象类。任何帮助/建议将不胜感激。

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++ - 'non-virtual interface' 和 'abstract interface' 有什么区别?

我正在用C++实现设计模式,我希望我的类通过组合来利用接口(interface),这让我研究了实现接口(interface)的不同方法。我想澄清一下这个术语的定义。 最佳答案 非虚拟接口(interface)是一个公共(public)成员函数,它不是虚拟的,但通常希望根据可覆盖的虚拟函数来实现:classInterface{public:intcompute(){returncompute_impl();}private:virtualintcompute_impl()=0;protected:virtual~Interface()

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++ - 如果没有类重新实现 'virtual' 关键字是否可以优化掉?

当我在C++中定义一个类时,我总是将dtor定义为虚拟的。这是我在编写继承类时保护自己的方法。我想知道我是否要支付性能开销,即使我不会继承该类。例如:classAfinal{A();virtual~A(){printf("dtor");}};当我使用这个类时,dtor实际上是通过vtable调用还是作为静态dtor实现? 最佳答案 WhenIdefineaclassinC++Ialwaysdefinethedtorasvirtual.这是非常糟糕的做法。类应该被设计成多态的……或者不是。这也不仅仅是设计问题——多态性增加了开销。现在