如何正确初始化switch语句不同的派生类?

问题描述 投票:2回答:2

我有下面的代码,用碱Base类的类层次结构,和几个派生类Derived1,Derived2的,Derived3 ......等等。

switch(i){
    case 1:{
        Derived1* d1;
        generateD1(d1);
        otherFunc(d1); //function signature is otherFunc(Base*)
        break;
    }
    case 2:{
        Derived2* d2;
        generateD2(d2);
        otherFunc(d2);
        break;
    }
    ...  //goes on for many cases
}

我如何使用继承机制,以改善上面的代码?

c++ pointers inheritance abstract dynamic-memory-allocation
2个回答
2
投票

事情是这样的:

class Base
{
    public:
        virtual ~Base() {}

        virtual void generate() = 0 {}

        virtual void other() = 0 {}
};

class Derived1 : public Base
{
    public:
        virtual void generate() override {}

        virtual void other() override {}
};

class Derived2 : public Base
{
    public:
        virtual void generate() override {}

        virtual void other() override {}
};

int main()
{
    int i;
    Base *b;
    switch(i)
    {
        case 1:
            b = new Derived1;
            break;
        case 2:
            b = new Derived2;
            break;
        ...
    }
    b->generate();
    b->other();
    ...
    delete b;
    return 0;
}

你可以放下generate()方法,只是使用的构造函数:

class Base
{
    public:
        virtual ~Base() {}

        virtual void other() = 0 {}
};

class Derived1 : public Base
{
    public:
        Derived1() {}

        virtual void other() override {}
};

class Derived2 : public Base
{
    public:
        Derived2() {}

        virtual void other() override {}
};

int main()
{
    int i;
    Base *b;
    switch(i)
    {
        case 1:
            b = new Derived1;
            break;
        case 2:
            b = new Derived2;
            break;
        ...
    }
    b->other();
    ...
    delete b;
    return 0;
}

2
投票

仅供参考,除了@SidS的溶液,我们也可以提取生成处理作为一个简单的函数,如下所示,它返回Base的指针。在这里,我也用std::unique_ptr这使得我们的代码与RAII语义更加安全,那么你可以省略调用delete b

#include <stdexcept>
#include <memory>

std::unique_ptr<Base> create(int i) // C++11
{
    switch(i)
    {
        case 1:
            return std::make_unique<Derived1>(); // C++14
            break;
        case 2:
            return std::make_unique<Derived2>();
            break;
        default:
            throw std::logic_error("unsupported.");
    }
}

然后发送方将如下更加简单:

DEMO

auto b = create(i);
b->generate();
b->other();
© www.soinside.com 2019 - 2024. All rights reserved.