简体   繁体   English

确保在编译时,派生自抽象基类的所有类均实现特定的构造函数签名

[英]Ensure at compile time that all classes derived from abstract base class implement specific constructor signature

I have an abstract base class, and multiple objects that might inherit from it. 我有一个抽象基类,以及可能从其继承的多个对象。 For instance 例如

    class Mode {
        public:
        int n,l,m;
        double omega;
        virtual int doSomething() =0;
        virtual ~Mode(){};
    }

    class ModeA : Mode {
        ModeA(int N, int L, int M);
        ModeA(double W, int L, int M);
        virtual ~ModeA();
        int doSomething();
    }

    class ModeB : Mode {
        ModeB(int N, int L, int M);
        ModeB(double W, int l, int M);
        virtual ~ModeB();
        int doSomething();
    }    

The two child classes, ModeA and ModeB have constructors with parameters (int, int, int) and (double, int, int) . 两个子类ModeAModeB具有带有参数(int, int, int)(double, int, int)构造函数。 Later, I have a piece of code that expects as Mode object, which looks like 后来,我有一段代码期望作为Mode对象,看起来像

    template <class MODE> int mode_finder(MODE** mode_list, int num_mode, int N, int L, int M){
        assert((std::is_base_of<Mode,MODE>::value));
        for(int i=0; i<num_mode; i++){
            mode_list[i] = new MODE(N,L,M);
             ... some more code here ...
        }
    }

(this isn't simply a function to initialize a list of modes, the ...some more code here... part is about 300 lines long and includes checking values against a list and deleting and recreating if they don't match -- a lot of stuff has been left out of this simple working example) (这不仅仅是初始化模式列表的函数, ...some more code here...部分大约有300行长,其中包括对照列表检查值,如果不匹配则删除并重新创建- -这个简单的工作示例中遗漏了很多东西)

If I'm using ModeA and ModeB , then this is fine, because they have the right kind of constructors. 如果我使用ModeAModeB ,那很好,因为它们具有正确的构造函数。

My problem is that I (or whoever inherits this code) might eventually make a new type and forget to put in constructors of this form, such as 我的问题是,我(或继承此代码的人)最终可能会创建一个新类型,而忘记放入这种形式的构造函数,例如

    class ModeC : Mode {
        ModeC(int X);
        virtual ~modeC();
        int doSomething();
    }

This will create run-time errors if I call mode_maker with ModeC , when the function template for ModeC is created. 如果我叫这将创建运行时错误mode_makerModeC ,当函数模板ModeC创建。

I want for every object to inherit from Mode to implement constructors with two signatures: a (int, int, int) and (double, int, int) form. 我想让每个对象都从Mode继承以实现具有两个签名的构造函数: (int, int, int)(double, int, int)形式。 If not, I want to throw a compile-time error. 如果没有,我想抛出一个编译时错误。

I assumed pure virtual constructors would do the trick, something like: 我假设纯虚拟构造函数可以解决问题,例如:

    virtual Mode(int, int, int) =0;
    virtual Mode(double, int, int) =0;

but apparently that isn't a language feature due to vtable issues. 但是显然由于vtable问题,这不是语言功能。 I don't really need entries on the vtable for anything, I just need the child classes to be forced to make constructors of this type. 我真的不需要vtable上的任何条目,我只需要强制子类构造这种类型的构造函数即可。

How can I do the thing I'm trying to do? 我该怎么做我想做的事情?

Thanks in advance. 提前致谢。

This will create run-time errors if I call mode_maker with ModeC, when the function template for ModeC is created. 如果创建ModeC的功能模板时,如果我使用ModeC调用mode_maker,则会产生运行时错误。

No it won't. 不,不会。 Templates are checked at compile time. 在编译时检查模板。

Template functions generate functions at compile time. 模板函数在编译时生成函数。 Template classes generate classes at compile time. 模板类在编译时生成类。

You may be confused by generics in Java/C#, which where inspired by and serve some of the purposes of templates, and are far more runtime based. Java / C#中的泛型可能会让您感到困惑,因为Java / C#的泛型受到模板的启发并满足模板的某些用途,并且它们更多地基于运行时。

C++'s equivalent of Generics is type erasure classes like std function. C ++的泛型等效项是类型擦除类,例如std函数。 This uses templates to create the glue code that Java/C# language does for you when you interact with Generics, roughly. 当您与泛型进行交互时,它使用模板来创建Java / C#语言为您执行的粘合代码。 I would consider doing this (creating type eraser helper types, as opposed to just using std function) to be an advanced use of C++. 我会考虑这样做(创建类型橡皮擦助手类型,而不是仅使用std函数)是C ++的高级用法。 There are some uses of Generics in Java/C# that require this in C++; Java / C#中有一些泛型用法,在C ++中需要这样做。 most uses.of Generics, however, can be emulated in C++ by bog standard template code. 然而,大多数的泛型用法可以通过沼泽标准模板代码在C ++中进行仿真。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 确保派生类构造函数必须调用特定的基类方法 - Ensure derived class constructor must call specific base class method 如何在从抽象基派生的类中实现相同的方法? - How to implement the same method in classes derived from an abstract base? 在抽象 class 构造函数中为所有未来的派生类强制执行初始化行为 - Enforce initialization behaviour in abstract class constructor for all future derived classes 您是否需要从所有派生类调用虚拟基类构造函数? 即使他们不是最衍生的? - Do you need to call virtual base class constructor from all derived classes? Even if they're not the most derived? 如何在指向具有派生类实例的基类的指针向量上实现派生类的副本构造函数? - How to implement a copy constructor of the derived class on a vector of pointers to base class which has instances of derived classes? 如何维护从基类派生的所有类型的编译时列表? - How can I maintain a compile-time list of all types derived from a base class? 难以实现类型抽象基 class 的 C++ 数组,同时其中有多个不同的派生类 - Difficulty implement a C++ array of type abstract base class while having multiple different derived classes in it 如何实现抽象基类的子类构造函数? - How to implement subclass constructor of abstract base class? 如何从 C++ 中的基础 class 获取所有派生类? - How to get all derived classes from a base class in C++? 如何授予对所有类的访问权,这些类都派生自基类 - How to give access to many classes, all derived from a base class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM