草庐IT

Inheritance

全部标签

c++ - 派生异常不继承构造函数

下面的代码有问题。#include#includeclassMyException:publicstd::logic_error{};voidmyFunction1()throw(MyException){throwMyException("fatalerror");};voidmyFunction2()throw(std::logic_error){throwstd::logic_error("fatalerror");};intmain(){try{myFunction1();//myFunction2();}catch(std::exception&e){std::coutthr

c++ - 多态性和默认值 : can co-exist?

这个问题在这里已经有了答案:virtualfunctiondefaultargumentsbehaviour(7个答案)关闭8年前。我有一个包含许多继承派生类的基类。像这样:classA{public:virtualvoidf(stringfoo="bar"){cout为简洁起见,我只继承了两个类。这是主要的:A*aArray[]={newB,newC,};intmain(){aArray[0]->f();aArray[0]->f();return0;}当我运行程序时,我得到的输出是:barbar就像编译器忽略重写函数的默认参数一样。这是正常现象,还是我做错了什么或遗漏了什么?

模板对象的 C++ 继承(G++ 编译器)

#include#include#includeusingnamespacestd;classCFirstLevel{public:CFirstLevel(conststring&_name):name(_name){}//...protected:stringname;};templateclassCSecondLevel:publicCFirstLevel{public:CSecondLevel(conststring&_name):CFirstLevel(_name){}virtualvoidPushBack(T)=0;virtualvoidPrint(intI){coutdat

C++多态和虚函数

是否可以从B调用虚函数foo(int)而无需使用注释中的内容?classA{public:virtualvoidfoo(char*){}virtualvoidfoo(int){}};classB:publicA{public:voidfoo(char*){}//voidfoo(inti){////A::foo(i);//}};Bb;b.foo(123);//cannotconvertargument1from'int'to'char*' 最佳答案 是的,这是可能的。这里的问题是函数B::foo(char*)隐藏继承函数A::foo(

C++ Qt 基类虚析构函数

我们是否需要一个用于Qt-way的类的虚拟析构函数:设置QObject-parent它将调用QObject的析构函数deleteLater()或对于它被设置为父对象的任何对象,是否有类似的东西?例如:classMyWidget:publicQWidget{public:MyWidget(){w=newQWidget(this);//"w"willbedeletedautomaticallybyparentMyWidget::QWidget::QObject'sdestructorafaik}private:QWidget*w;}如果MyWidget类要被继承,我们是否需要一个虚拟析构函

c++ - 虚拟继承的价格是多少?

这似乎是一个基本问题,但我没有看到它被问到:假设以下简单情况:没有虚拟成员。虚拟继承用于允许多个路径指向同一基。就访问最派生类的成员所需的时间而言,虚拟继承的代价是多少?特别是,如果价格不为零,它是仅适用于通过多条路径继承的成员还是也适用于其他成员? 最佳答案 Whatisthepriceofvirtualinheritanceintermsofthetimeneededtoaccessthemembersofthemostderivedclass?一个偏移查找和一个加法(2条指令和一个内存获取)Inparticular,ifthe

c++ - 友元和私有(private)嵌套类继承

我想继承嵌套类,它位于外部类的私有(private)部分。可能吗?classA{friendclassB;friendclassC;private:classNiceNestedClass{};};classC{voida(){A::NiceNestedClassworks;}};classB:A::NiceNestedClass{};NiceNestedClass的实例化不是问题。但是g++不允许我继承它。有什么解决方法吗?g++-std=c++11a.c-oaa.c:5:11:error:‘classA::NiceNestedClass’isprivateclassNiceNest

c++ - 对C++多重继承感到困惑

我对C++的更高级功能有些陌生。昨天,我发布了以下问题,了解了虚拟继承和可怕的死亡钻石。InheritingfrombothaninterfaceandanimplementationC++我还通过其他链接了解到,多重继承通常是错误代码设计的标志,并且通常可以在不使用MI的情况下更好地实现相同的结果。问题是……对于以下问题,我不知道什么是更好的单继承方法。我想为两种类型的数字点定义一个接口(interface)。输入数字点和输出数字点。界面要简洁,只有访问信息所需的内容。当然,绝大多数属性对于这两种类型的数字点都是通用的。所以对我来说,这是一个明显的继承案例,而不是组合。我的接口(in

C++ : union of two types without virtual base class inheritance

是否可以在不手动创建交集类型的情况下创建两种类型的并集?问题是在我的上下文中交集类是完全没有意义的,所以创建它会使代码用户感到困惑。我的实际案例:我正在描述一个数字硬件模拟器,它是许多模块的分层树状结构:classport;classmodule0{porta,b,c;}classmodule1{portc,d,e;}我需要创建这两种类型的union:classtop_level_module{porta,b,c,d,e;}我想应该有一些技术来创建union类型(这是我要问的问题):classtop_level_module:union_type{//porta,b,c,d,e;}但是

c++ - 有条件地继承自纯基类

假设我有以下类定义structbase{virtualintf()=0;};structA:publicbase{intf()final{return1;}};structB:publicbase{intf()final{return2;}};是否可以将A和B转换为带有bool参数的模板,该参数指定是否从base继承还是不?我有需要或不需要提供通用接口(interface)的基类的用例。假设A和B有很多成员函数,那么重复实现会很乏味。但是sizeof(A)和sizeof(B)很小。 最佳答案 当然:templatestructA{/