草庐IT

C++ STL:将派生虚拟类用作 std::sort() 的 "Strict Weak Ordering"

我使用std::sort()撞墙了。我有一个纯虚类(名为Compare),方法的调用者派生自该类(名为MyComp)。我将纯虚拟类用于我的API原型(prototype):voidObject::DoSort(Compare&comp){std::sort(this->mKeys.begin(),this->mKeys.end(),comp);}来电者:classMyComp:publicCompare{booloperator()(constRow*r1,constRow*r2){...}}cmp;...obj->DoSort(cmp);Linux上的g++编译器提示:“无法分配类型

c++ - 如何在编译时验证模板类是从给定类派生的?

我想知道,是否有任何优雅的方法(如this)来检查模板参数是否派生自给定的类?一般而言:templateclassMyClass{//sholdgivethecompilationerrorifBisnotderivedfromA//butshouldworkifBinheritsfromAasprivate}另一个question中提供的解决方案仅当B作为公共(public)继承自A时才有效:classB:publicA然而,我宁愿没有这样的约束:classA{};classB:publicA{};classC:privateA{};classD;MyClass//worksnowM

c++ - 如何将 MPI 派生数据类型用于 3D 数组?

我想编写一个在3D矩阵上运行的并行代码,其中每个进程都有自己的子矩阵,但为了完成他们的工作,他们需要一些关于其相邻进程的子矩阵(只是边界平面)的信息。我通过点对点通信发送这些信息,但我知道对于大型矩阵来说这不是一个好主意,所以我决定使用派生数据类型进行通信。我对mpi_type_vector有疑问:例如,我有一个NX*NY*NZ矩阵,我想将常量NY的平面发送到另一个进程我为此写下了这些行:MPI_Datatypesub;MPI_Type_vector(NX,NZ,NY*NZ,MPI_DOUBLE,&sub);MPI_Type_commit(&sub);但它不起作用(无法发送我想要的飞机

c++ - 隐式构造函数可用于从 Base 派生的所有类型,但当前类型除外?

以下代码总结了我的问题:templateclassBase{};templateclassDerived1:publicBase{};templateclassDerived2:publicBase{public://CopyconstructorDerived2(constDerived2&x);//AnEXPLICITconstructorthatdoesaspecialconversionforaDerived2//withothertemplateparameterstemplateexplicitDerived2(constDerived2&x);//Nowtheproble

c++ - 从基类识别派生类

有什么方法可以检查两个实例是否是同一个派生类?像这样的东西:Base*inst1=newA();Base*inst2=newB();Base*inst3=newA();boolb1=(inst1->class==inst2->class);//class==inst3->class);//显然我可以只向基类添加一个虚函数并实现每个派生类以返回一个唯一值。但是,我宁愿不必为派生类实现任何特定的东西,因为我正在制作一个基于派生自这个基类的API。 最佳答案 typeid(*inst1)==typeid(*inst2)假设Base至少有一

C++ 多重继承,基类派生自同一个类

我在尝试重用来自不同类的代码时偶然发现了一个问题。我把它贴在这里,希望你们中的一些人能够帮助我。我有一组类(B,C)派生自同一个类(A),它强制执行某些方法(foo,run)。B类实现了这些方法,B类和C类都提供了其他方法:#includetemplateclassA{public:A(){}virtual~A(){}virtualvoidfoo()const=0;//forceimplementationofthisfunctionvirtualvoidrun()const=0;//forceimplementationofthisfunction};templateclassB:p

c++ - 派生自模板基类的模板构造函数

只是好奇,是否有可能从模板类继承并在派生类的构造函数中调用基类的构造函数,该基类也是模板化的并且没有参数可从中推断其类型?templatestructBase{templateBase(){//noargumentoftypeDtoinferfromstatic_assert(std::is_same::value,"");}};structDerived:Base{Derived():Base::Base(){}//isthereawaytowriteitcorrectly?};在我的特定情况下,我可以用模板方法替换模板构造函数,但这仍然是一个关于语言灵active的有趣问题。

c++ - 如何使数组的派生类型接受聚合初始化?

例如classA:publicstd::array{};和Aa{1,2,3};//failedcurrently.如何让数组的派生类型接受聚合初始化? 最佳答案 您可以提供一个可变模板构造函数,如下所示:classA:publicstd::array{public:templateconstexprA(Args&&...args):std::array{{std::forward(args)...}}{}};LiveDemo编辑:以下版本也适用于VisualStudio:classA:publicstd::array{public:

c++ - 您是否需要从所有派生类调用虚拟基类构造函数?即使它们不是最衍生的?

我在多重继承和菱形问题上遇到了麻烦。出现问题是因为我的基类构造函数需要一个参数。编译器尝试为我的两个抽象类生成默认构造函数,但失败了,因为默认构造函数无法确定基类的参数。我不明白为什么我的抽象类要调用基本构造函数。我认为最派生的类是调用虚拟基类构造函数的类。这是重现我所说内容的代码:classVirtualBase{public:VirtualBase(intinitial):count(initial){}intgetCount()const{returncount;}voidincrement(){count++;}private:intcount;};classContractA

c++ - 在派生构造函数中访问基本成员时出现问题

给定以下类:classFoo{structBarBC{protected:BarBC(uint32_taKey):mKey(aKey)mOtherKey(0)public:constuint32_tmKey;constuint32_tmOtherKey;};structBar:publicBarBC{Bar(uint32_taKey,uint32_taOtherKey):BarBC(aKey),mOtherKey(aOtherKey)//Compileerrorhere};};我在指示的位置遇到编译错误:error:class`Foo::Bar'doesnothaveanyfieldn