草庐IT

work_base

全部标签

c++ - 同名类之间的共享 vtables : call to virtual method crashes when casting to base type

检查下面的更新,我可以重现并需要帮助。我有一个奇怪的崩溃,其中一些方法在除1个地方之外的任何地方都可以正常工作。这是代码:structbase{virtualwchar_t*get()=0;//canbe{returnNULL;}doesn'tmatter};structderived:publicbase{virtualwchar_t*get(){returnSomeData();}};structcontainer{deriveddata;};//thisisapprox.howitisusedinrealprogramvoidoutput(constbase&data){data

c++ - 错误 : Range-based 'for' loops are not allowed in C++98 mode

所以我正在关注此页面上的教程:http://www.cplusplus.com/doc/tutorial/control/但是我在做范围/基于for循环时遇到了麻烦。我找到了这个页面:GNUGCCcompilerupdating那里的答案说我应该打开“项目”和“属性”。但是当我尝试这样做时,“属性”选项变灰了,没有任何解释:http://imageshack.com/a/img571/4371/xd1x.png那么..我怎样才能激活范围/基于for循环? 最佳答案 将-std=c++11标志传递给编译器。当然,GCC应该足够新(>=

C++ 方法重载 : base and derived parameters

在网上搜索后,我没有设法找到这个问题的答案:我有这个重载方法:foo(Base*base);foo(Derived*derived);在这种情况下,“Derived”是“Base”的子类。当我打电话时:foo(newDerived());我注意到总是调用第一个重载方法,而我想获得相反的结果(调用以“Derived*”对象作为参数的方法)。如何解决?谢谢。编辑:好的,这是我的实际情况:我有一个UIWidget和一个UIScoreLabel类。UIScoreLabel派生自UIWidget。我还有一个GameEvent类(Base)和一个P1ScoreGameEvent类(Derived)

c++ - 为什么 'Base Class object' 不能调用它自己的虚函数? C++

我已经阅读了C++中的虚函数,并了解它们如何为程序员提供使用基类指针访问派生类成员函数的权限。(又名多态性)。一直困扰我的问题是:为什么要在基类中声明一个同名函数,如果最后它必须被声明为虚函数?(注意:我需要有关虚函数多态性方面的答案)在下面的代码中,如果使用基类指针调用“virtualdisplay()”(第22行),则会显示错误。为什么C++中的虚函数如此严格w.r.t.没有被基类指针调用?.#includeusingnamespacestd;classB{public:voidvirtualdisplay(){coutdisplay();Whycan't'b'callit'sow

c++ - 为什么friend类可以通过Derived类访问到Base类私有(private)数据

这是我第一次在这里发帖。classBase{private:intbase;friendclassQuestion;};classDerived:publicBase{private:intsuper;};classQuestion{public:voidtest(Base&base,Derived&derived){intvalue1=base.base;//Noproblem,becauseQuestionisafriendclassofbaseintvalue2=derived.super;//Compileerror,becauseQuestionisnotafriendcla

c++ - 意外的 std::io_base::failure 异常

这个问题在这里已经有了答案:Unexpectedexceptioninstd::ifstream(1个回答)关闭7年前。以这个简单的程序为例:#includeintmain(){std::ifstreamin(".");intx;if(in)in>>x;}在Redhat6、gcc4.4.7上运行没有错误在Ubuntu14.04LTS上,gcc4.8.2运行没有错误在Redhat7上,gcc4.8.2我得到:terminatecalledafterthrowinganinstanceof'std::ios_base::failure'what():basic_filebuf::under

c++ - 从C++模板特化那 "base"调用 "overrides"模板函数呢?

问题:有没有一种方法可以从C++中的专用模板函数调用“基本”模板函数,子类可以在覆盖它们时访问父类的虚拟方法版本?(注意:我怀疑答案是“否”,但很想是错误的)上下文:我经常发现自己专门化模板函数只是因为特殊情况需要额外的预处理或后处理,而不是因为代码的“内脏”发生了变化。举个例子:通过继承,您可以执行以下操作:structbase{virtualvoidgo(){printf("%p",this);}};structfoo:base{virtualvoidgo(){printf("thisfoolivesat");base::go();}};...调用foo::go()将打印“this

c++ - 非常量大小的数组 : Why does this even work?

这个问题在这里已经有了答案:C++:Whydoesintarray[size]work?(3个答案)关闭8年前。#includeusingnamespacestd;intmain(){intn;cout>n;intarray[n];//I'veworkedsomeoutputsanditworksreturn0;}这是某种动态分配吗?为什么它甚至不给出“n”为“const”的错误?另外,写cout不会导致编译时或运行时错误。我正在使用Dev-C++。

c++ - 错误 : Invalid base class C++

谁能解释一下是什么导致了这个错误?Error:Invalidbaseclass我有两个类,其中一个派生自第二个:#if!defined(_CGROUND_H)#define_CGROUND_H#include"stdafx.h"#include"CGameObject.h"classCGround:publicCGameObject//CGameObjectissaidtobe"invalidbaseclass"{private:boolm_bBlocked;boolm_bFluid;boolm_bWalkable;public:booldraw();CGround();CGround

c++ - 非常基本的 : Why won't my makefile work with common suffix

objects=hello.oname.oprinting.oexename=himake$(exename):$(objects)$(CC)-o$(exename)$(objects)%.o:%.cpp$(CC)-c$^我正在尝试使用通用后缀,因此我不需要先将3个文件编译成.o。这应该使用%通配符完成所有这三个操作。当我做很长的路时它工作正常但不是这个。运行上面的makefile会出现以下错误:[alex@pccDir]$makecc-ohimakehello.oname.oprinting.ohello.o:Infunction`__static_initialization_an