草庐IT

c++ - 在编译时查找基类

标题几乎说明了一切:在C++中有没有一种方法可以在编译时获取类的基类型?IE。是否可以将一个类传递给一个模板,然后让该模板使用它传递给定类的基类的其他模板?我的问题不是我是否可以自己实现这样的功能,毫无疑问我可以(使用特征等)。我的问题是是否有一些(晦涩的)内置功能可用于此目的。 最佳答案 gcc支持这一点。见Kerrek'sanswertr2/type_traitsAndyProwl'scodeexamplen2965WhatisthestatusofN2965-std::basesandstd::direct_bases?How

c++ - 从基类继承两次时如何修复 "error: no matching function for call to"

我目前正在尝试在我的项目中实现一系列继承类。因此,我使用成员初始值设定项列表并将对变量的引用“管道化”到基类。我真的不确定,为什么我会收到编译器错误。我已经尝试将引用“int&id”更改为指针“int*id”。上面的示例只是指出我的问题的最小示例:classBase{public:int&m_id;Base(int&id):m_id(id){}};classDerived1:virtualpublicBase{public:Derived1(int&id):Base(id){};};classDerived2:publicDerived1{public:Derived2(int&id)

c++ - 我应该从私有(private)派生类指针转换到它的基类吗?

我从C++FAQ中找到的Generally,No.Fromamemberfunctionorfriendofaprivatelyderivedclass,therelationshiptothebaseclassisknown,andtheupwardconversionfromPrivatelyDer*toBase*(orPrivatelyDer&toBase&)issafe;nocastisneededorrecommended.HoweverusersofPrivatelyDershouldavoidthisunsafeconversion,sinceitisbasedonapr

c++ - 如何从基类中暴露隐藏的重载?

给定这段代码:classbase{public:stringfoo()const;//Wantthistobevisiblein'derived'class.}classderived:publicbase{public:virtualintfoo(int)const;//Causesbaseclassfoo()tobehidden.}如何使base::foo()对派生可见而不用调用基类的虚拟方法重载来复制它?using是否可以解决问题,如果可以,它去哪里了,是这样的吗?classderived:publicbase{public:virtualintfoo(int)const;usi

c++ - "using"与基类名称更改访问有效吗?

我的friend给我看了下面的代码structA{virtualvoidf()=0;virtualvoidg()=0;};structAInternal:A{virtualvoidf(){/*...*/}virtualvoidg(){/*...*/}};他使用AInternal作为实现大部分(如果不是全部A)的内部类。然后他从AInternal继承,但由于他希望AInternal保持不可访问(因为它是一个实现细节),他继承了protected(根据...实现)。他还做的是使用基类名称使A可访问(默认情况下它是protected,因为AInternal也被继承保护)structC:pro

c++ - C++03 12.4/12 中关于通过指针显式调用基类析构函数的说法是什么?

根据C++0312.4/12当显式调用析构函数时iftheobjectisnotofthedestructor’sclasstypeandnotofaclassderivedfromthedestructor’sclasstype,theprogramhasundefinedbehavior所以我有这段代码:classBase{};classDerived:publicBase{};charmemory[100];new(memory)Derived();Base*ptr=(Base*)memory;ptr->~Base();这里对象的类型是Derived并且“析构函数的类类型”是Ba

c++ - 我可以对来自不同基类的相同功能实现不同的实现吗?

假设如下:classA{virtualvoidf()=0;};classB{virtualvoidf()=0;};我能否以某种方式执行以下操作?classC:publicA,publicB{virtualvoidA::f(){printf("f()fromA");}virtualvoidB::f(){printf("f()fromB");}};那么现在我可以做什么了???A*pa=newC();pa->f();//printsf()fromA;B*pb=(B*)pa;pb->f();//printsf()fromB;谢谢!!! 最佳答案

c++ - 基类的模板函数重载

这个问题在这里已经有了答案:PrioritywhenchoosingoverloadedtemplatefunctionsinC++(6个答案)关闭3年前。我如何强制编译器为基类选择模板函数重载?举个例子说明问题#includeclassA{};classB:publicA{};templatevoidf(constT&t){std::coutvoidcall_f(constT&t){f(t);}intmain(){call_f(10);call_f(A());call_f(B());return0;}它产生输出GenericfOverloadforAGenericf为什么编译器不接收

c++ - 继承虚基类的构造函数

虚基类在最派生类中初始化,所以我的猜测是继承基类的构造函数应该也能工作:structbase{base(int){}};structderived:virtualbase{usingbase::base;};derivedd(0);然而,这无法用GCC5.2.0编译,它试图找到base::base(),但在Clang3.6.2上工作正常。这是GCC中的错误吗? 最佳答案 这是gcc错误58751“[C++11]继承构造函数不能与虚拟继承一起正常工作”(又名:63339“使用来自虚拟基的构造函数”被隐式删除”):来自58751的描述:

c++ - 虚拟继承是否强制基类默认可构造?

在下面的代码中,编译器要求基类X是默认可构造的。但是,如果我从classNode的继承中删除virtual关键字,则对成员m_x的访问当然会变得不明确,但不再需要类X的默认构造函数。这是什么原因?#includestructApply{templatestructNode:virtualT//thislinecontainsthevirtualinheritance{templateNode(Args...args):T(args...){}};templatestructInheritance;templatestructInheritance:FirstBaseClass,Inhe