草庐IT

c++ - 从模板基类派生的类的隐式转换

我在隐式转换、模板和模板类继承方面遇到了问题。以下是我从我的项目中提取的内容,我省略了一些类甚至是抽象的,但这与大小写无关。classA{};classB:publicA{};templateclassBase{};classDerived:publicBase{};intmain(){Derivedd;Base*base=newDerived();}基本上,我有一个模板基类Base我得出Derived:publicBase从。然后我必须将它转换为最常见的Base形式,即Base.我原以为我可以转换一个派生自Base的对象至Base隐含地,作为B源自A.我做错了什么或者我怎么能隐式?这

c++ - 将模板方法移动到派生中断编译

给定以下代码:templateclassA{public:Tt;};classB{public:voidfoo(inti){}templatevoidfoo(A&a){}};intmain(){Aa;Bb;b.foo(a);b.foo(a.t);}这可以编译并且工作正常;B::foo()的正确重载版本被选择并为a和a.t调用。现在我引入一个新的类C,它派生自B并将::foo()的模板版本移出B并进入C:templateclassA{public:Tt;};classB{public:voidfoo(inti){}};classC:publicB{public:templatevoidf

c++ - 如何在抽象类中声明重载运算符并在派生的非抽象类中覆盖它?

我正在尝试编写一个带有一些纯虚拟二元运算符的抽象类,这些运算符应该由派生类实现,以实现运算符多态性。这是一个简化的示例:classBase{public:virtualconstBase&operator+(constBase&)const=0;};classDerived:publicBase{public:constDerived&operator+(constDerived&)const;};constDerived&Derived::operator+(constDerived&rvalue)const{returnDerived();}现在运算符做什么并不重要,重要的是它返回

c++ - 在 CRTP 类中使用派生类型的成员

我有一个奇怪的重复出现的模板模式类和一个像这样的派生类:templateclassA{typedeftypenameDerived::CD;Dx;};classB:publicA{public:classC{};};编译失败是因为当编译器试图定义D时B没有被完全定义。我怎样才能获得类似的结果,即A的成员属于B中定义的类型?或者我是否必须强制在B之外定义C? 最佳答案 OrdoIhavetoforceCtobedefinedoutsideofB?是的,不幸的是你必须这样做。通常你可以在A之前定义一个模板类,并将它专门用于B,包含C类型

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++ - 为什么我的 Curiously Recurring Template Pattern (CRTP) 不能引用派生类的 typedef?

这个问题在这里已经有了答案:C++staticpolymorphism(CRTP)andusingtypedefsfromderivedclasses(5个答案)关闭9年前。使用curiouslyrecurringtemplatepattern时,如果我试图从基类中引用属于派生类的typedef,则仅无法引用它们;gcc提示notypenamed'myType'inclassDerived.这似乎与使用typedef、模板和奇怪的重复关系的其他方式不一致。考虑:/*crtp.cpp*/#includeusingnamespacestd;//case1.simple.classBase{

c++ - 通过指向基类对象的指针使用指向派生类对象的指针进行操作

我有这个代码代表银行:classBank{friendclassInvestmentMethod;std::vectoraccounts;public://...BaseBankAccount是银行中所有账户的抽象类:classBaseBankAccount{public:BaseBankAccount(){}virtualintgetInterest()const=0;virtualintgetInvestedSum()const=0;virtualvoidincreaseDepositSum(intadditionalSum)=0;virtualvoidclose(std::str

c++ - 派生类是否被视为 friend ?

如果我创建基类A并且A是类B的友元,那么从A派生的类是否可以按自己的喜好访问B,否则它允许什么?谢谢 最佳答案 structA{};structAder:A{};structB{friendstructA;};没有。在C++中,友元不是继承的。它也不具有传递性。Ader不能作为friend访问B除非B明确给予友元,只是因为它的基础A是的friend>B. 关于c++-派生类是否被视为friend?,我们在StackOverflow上找到一个类似的问题: htt

使用将在派生构造函数中构造的参数调用的 C++ 基本构造函数

问题1)classBase{Base(std::stringname);virtualstd::stringgenerateName();}classDerived:Base{Derived();virtualstd::stringgenerateName();}问题来了:将在generateName()上调用什么方法?Derived::Derived:Base(generateName()){//whatmethodwillbecalledongenerateName()?}问题2)我应该怎么做?如果默认构造函数必须接受一个参数,但我需要在Derived构造函数中生成该参数?

c++ - QObject 派生类型是否需要父 QObject?

我正在编写一些派生自QObject的Qt类,它看起来像:classA:publicQObject{Q_OBJECTpublic:A():QObject(){}.....}但是我在几个地方看到,QObject派生类都有一个父类,例如:classA:publicQObject{Q_OBJECTpublic:A(QObject*parent=0):QObject(parent){}.....}所以问题是:我是否需要parent?如果我有一个,如果我有一个默认的(0)或者我根本没有,有什么区别? 最佳答案 因此你不需要parent。但是设置