草庐IT

c++ - 为什么允许 std::atomic_{char,schar,etc.} typedef 是 std::atomic<T> 基类的类型定义,而不仅仅是 atomic<T>?

C++11[atomics.types.generic]p7:Thereshallbenamedtypescorrespondingtotheintegralspecializationsofatomic,asspecifiedinTable145,andanamedtypeatomic_boolcorrespondingtothespecifiedatomic.Eachnamedtypeisaeithertypedeftothecorrespondingspecializationorabaseclassofthecorrespondingspecialization.Ifitisa

c++ - 访问基类的 protected 构造函数

派生类可以在其ctor-initializer中调用protected基类构造函数,但仅限于其自己的基类子对象,而不能在其他地方调用:classBase{protected:Base(){}};classDerived:Base{Baseb;public:Derived():Base(),//OKb(){//errorBaseb2;//error}};标准对此有何规定?这是[class.protected]/1:AnadditionalaccesscheckbeyondthosedescribedearlierinClause11isappliedwhenanon-staticdata

c++ - 在依赖基类中找到的名称是否应该在实例化时隐藏命名空间名称?

使用以下代码,clang3.0给出error:lookupof'N'inmemberaccessexpressionisambiguous,而clang3.4和gcc4.8都接受代码而没有错误。structB{structM{voidf(){}};};namespaceN{structM{voidf(){}};}templatestructA:N::M,B::M{typedefBN;};structD:A{Am;voidf(){m.N::M::f();//foundclass-name'A::N'(unambiguous)}};templatestructC:A{Am;voidf(){

c++ - 当基类不包含数据成员时是否仍然需要虚拟继承?

以下代码是否仍会因缺少虚拟继承而受到负面影响?如果是这样,如果classAdid,负面影响是否与没有虚拟继承的多重继承的负面影响相同(或同样糟糕)包含数据成员?classA{public:virtual~A(){}virtualintfoo()const=0;};classB:publicA{public:virtual~B(){}};classC:publicA{public:virtual~C(){}};classD:publicB,publicC{public:virtualintfoo()const{return12;}}; 最佳答案

c++ - 确定派生类是否覆盖了基类的方法

classB{virtualintfoo();};classD:publicB{virtualintfoo(){coutfoo()isoverriddenbyasubclass,orifitwill*//*simplyrecurseintoB::foo()?*/this->foo();}main(){Dd;d.B::foo();} 最佳答案 回答:你不能。如果有什么可以扩展的,我会扩展。 关于c++-确定派生类是否覆盖了基类的方法,我们在StackOverflow上找到一个类似的问题:

c++ - 派生类不能访问基类的 protected 成员

考虑下面的例子classbase{protected:intx=5;int(base::*g);};classderived:publicbase{voiddeclare_value();derived();};voidderived::declare_value(){g=&base::x;}derived::derived():base(){}据了解,只有基类的friend和派生类可以访问基类的protected成员,但在上面的示例中,我得到以下错误"ErrorC2248'base::x':cannotaccessprotected在类中声明的成员但是当我添加以下行时friendcl

c++ - 为什么声明私有(private)基类会使类型名称不可访问?

令我感到惊讶的是,在以下示例中声明Middle的基类私有(private)使得该名称在后续派生中无法作为类型使用。classBase{public:Base(Baseconst&b):i(b.i){}inti;};classMiddle:privateBase{//因此使用g++(Debian6.3.0-18+deb9u1)6.3.020170516...编译g++-std=c++11privateBase.cpp我得到以下诊断信息:privateBase.cpp:15:9:error:‘classBaseBase::Base’isinaccessiblewithinthisconte

c++ - 指向抽象模板基类的指针?

我想不通。我需要一个抽象模板基类,它是以下内容:templateclassDendrite{public:Dendrite(){}virtual~Dendrite(){}virtualvoidGet(std::vector&o)=0;protected:std::vector_data;};现在,我从中得出Dendrite的具体用法。现在是问题。如何创建指向没有特定类型的基类的指针vector,这我想通过稍后将元素推送到它来指定?像这样的东西:classFoo{public:...private:std::vector_inputs;//!())and//!_inputs.push_b

c++ - 什么时候以及为什么不应该将基类中的析构函数定义为虚拟的?

下面的这个例子说明了如何防止派生类被复制。它基于一个基类,其中复制构造函数和复制赋值运算符都被声明为private。classUncopyable{protected://allowconstructionanddestructionofderivedobjects...Uncopyable(){}~Uncopyable(){}private://butpreventcopying...Uncopyable(constUncopyable&);Uncopyable&operator=(constUncopyable&);};我们可以使用这个类,结合私有(private)继承,使类不可复

c++ - 指向基类的指针总是 <= 指向派生类的指针吗?

我想知道C++标准是否保证单一继承使对象向上“增长”,即classBase和一个classDerived:publicBase,和一个指针Derived*ptr,dynamic_cast(ptr)的结果将始终在数值上小于或等于ptr. 最佳答案 没有。内存布局是实现细节。也就是说,假设没有虚函数,我不知道有任何实际不这样做的实现。如果您在Derived中引入一个虚函数(但在Base中没有),虚表指针可以(取决于实现)放在Base字段之前(使Base*大于Derived*)。澄清:上面的示例是特定于VisualC++的。您可以使用以下