C++ 通过构造函数参数实例化子类

C++ 通过构造函数参数实例化子类,c++,inheritance,constructor,C++,Inheritance,Constructor,我目前正在尝试实现一个接口来创建排序算法的实例 我有以下课程: ISortAlgorithm->抽象(接口“类) AlgorithmModule->带有静态函数-> static ISortAlgorithm* CreateSortInstanceOf(EAlgorithm AlgorithmEnum) 以及一个类容器(名称空间算法),其中包含类似-> class SelectionSort : public ISortAlgorithm 对于每个实现的算法也存在枚举-> enum EAlg

我目前正在尝试实现一个接口来创建排序算法的实例

我有以下课程:
ISortAlgorithm->抽象(接口“类)
AlgorithmModule->带有静态函数->

static ISortAlgorithm* CreateSortInstanceOf(EAlgorithm AlgorithmEnum)
以及一个类容器(名称空间算法),其中包含类似->

class SelectionSort : public ISortAlgorithm
对于每个实现的算法也存在枚举->

enum EAlgorithm {SELECTIONSORT, BUBBLESORT, ...}
运行时,如果有人想使用我模块中的算法,他会调用:

AlgorithmModule::CreateSortInstanceOf(/*enum of desired algorithm */)
我在该函数中做的第一件事是->

{
switch (enumparam)
{
case (EAlgorithm::INSERTSORT) :
        return SortAlgorithm = new Algorithms::InsertSort();
        break;
case (blah): [..]
}
这已经起作用了。但现在我想了一个更简单的方法,我想出了一个想法,我可以使用构造函数来实现这一点,并尝试:

class InsertSort : public ISortAlgorithm
{
public:
    InsertSort() : ISortAlgorithm(EAlgorithm::INSERTSORT){}
}

class SelectionSort : public ISortAlgorithm
{
public:
    SelectionSort() : ISortAlgorithm(EAlgorithm::SELECTIONSORT) {}
}
除此之外,我还将CreateSortInstanceOf修改为:

这样做的目的是,使用构造函数param调用正确的子类。这意味着我不必为将来要实现的任何算法修改此函数的代码。但是,当然编译器会抱怨,我无法实例化抽象类,我认为另一个问题是非继承性康托尔的

但是我确信,我的意图是可能的,所以我需要你的帮助来指出我在这里遗漏了什么


致以最诚挚的问候

这是一种做你想做的事的方式,即使它不一定是最好的:



    #include "stdafx.h"
    #include "map"

    enum EAlgorithm { SELECTIONSORT=0, INSERTSORT=1 };

    class ISortAlgorithm;
    typedef ISortAlgorithm * (*fct)(void);
    std::map mapCreator;

    class ISortAlgorithm
    {
        public:
        ISortAlgorithm(void) {};
        virtual void run(void) = 0;
        static ISortAlgorithm* CreateSortInstanceOf(EAlgorithm AlgorithmEnum)
        {
            std::map::iterator it = mapCreator.find(AlgorithmEnum);
            if (it == mapCreator.end())
                return NULL;
            return it->second();
        }
    };

    class InsertSort : public ISortAlgorithm
    {
        public:
        InsertSort(){}
        virtual void run(void){};
        static ISortAlgorithm * Create(void)
        {
            return (ISortAlgorithm*) new InsertSort();
        };
    };

    class SelectionSort : public ISortAlgorithm
    {
        public:
        SelectionSort(){};
        virtual void run(void){};
        static ISortAlgorithm * Create(void)
        {
            return (ISortAlgorithm*) new SelectionSort();
        };
    };

    int _tmain(int argc, _TCHAR* argv[])
    {
        mapCreator.insert(std::pair(EAlgorithm::INSERTSORT, InsertSort::Create));
        mapCreator.insert(std::pair(EAlgorithm::SELECTIONSORT, SelectionSort::Create));
        ISortAlgorithm * pt1 = ISortAlgorithm::CreateSortInstanceOf(EAlgorithm::INSERTSORT);
        ISortAlgorithm * pt2 = ISortAlgorithm::CreateSortInstanceOf(EAlgorithm::SELECTIONSORT);
        return 0;
    }


    #include "stdafx.h"
    #include "map"

    enum EAlgorithm { SELECTIONSORT=0, INSERTSORT=1 };

    class ISortAlgorithm;
    typedef ISortAlgorithm * (*fct)(void);
    std::map mapCreator;

    class ISortAlgorithm
    {
        public:
        ISortAlgorithm(void) {};
        virtual void run(void) = 0;
        static ISortAlgorithm* CreateSortInstanceOf(EAlgorithm AlgorithmEnum)
        {
            std::map::iterator it = mapCreator.find(AlgorithmEnum);
            if (it == mapCreator.end())
                return NULL;
            return it->second();
        }
    };

    class InsertSort : public ISortAlgorithm
    {
        public:
        InsertSort(){}
        virtual void run(void){};
        static ISortAlgorithm * Create(void)
        {
            return (ISortAlgorithm*) new InsertSort();
        };
    };

    class SelectionSort : public ISortAlgorithm
    {
        public:
        SelectionSort(){};
        virtual void run(void){};
        static ISortAlgorithm * Create(void)
        {
            return (ISortAlgorithm*) new SelectionSort();
        };
    };

    int _tmain(int argc, _TCHAR* argv[])
    {
        mapCreator.insert(std::pair(EAlgorithm::INSERTSORT, InsertSort::Create));
        mapCreator.insert(std::pair(EAlgorithm::SELECTIONSORT, SelectionSort::Create));
        ISortAlgorithm * pt1 = ISortAlgorithm::CreateSortInstanceOf(EAlgorithm::INSERTSORT);
        ISortAlgorithm * pt2 = ISortAlgorithm::CreateSortInstanceOf(EAlgorithm::SELECTIONSORT);
        return 0;
    }