Dynamic Class name?

Hi I just wondering if there's a way in C++ to get classes dynamically which have the same base class?

I mean, instead of creating switch statement to create multiple classes, I would like to use a single line to create any of these classes.

eg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//Instead of:

switch(class_type)
{
     case type_1:
         SubClass_1 obj = new SubClass_1();

     case type_2:
         SubClass_2 obj = new SubClass_2();

     case type_3:
         SubClass_3 obj = new SubClass_3();

     case type_4:
         SubClass_4 obj = new SubClass_4();

     case type_5:
         SubClass_5 obj = new SubClass_5();
}

//I just want to use a single line like this:

BaseClass obj = new (getClass(class_type))();


Something like that.

Can we do this in C++?

Thanks.

-FaRHaN-
Last edited on
closed account (3qX21hU5)
A factory pattern would work nicely here. Basically you have a abstract class for a factory that will be your interface, then you just have to create different subclasses for that abstract class for all the objects.

Here is a great post on SO about it.

http://stackoverflow.com/questions/4007382/how-to-create-class-objects-dynamically

And here is a nice piece on different kinds of factories http://www.codeproject.com/Articles/3734/Different-ways-of-implementing-factories
Last edited on
Thanks Zereo.

It seems that we still need to list out all of the classes and use the factory to get the class that we need.

I guess there's no shorter way to create the dynamic class.
Unfortunately, you have to do the bookkeeping/write the boilerplate code somewhere. Using a "Factory" doesn't absolve you of that task.

Here's something that's close to what you describe, semantically.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <functional>
#include <vector>
#include <memory>
#include <random>

struct Base
{
    virtual void print() const = 0;
    virtual ~Base() {}
};

struct D1 : public Base { void print() const { std::cout << "D1\n"; } };
struct D2 : public Base { void print() const { std::cout << "D2\n"; } };
struct D3 : public Base { void print() const { std::cout << "D3\n"; } };
struct D4 : public Base { void print() const { std::cout << "D4\n"; } };
struct D5 : public Base { void print() const { std::cout << "D5\n"; } };

template <typename T>
T* new_class()
{
    return new T;
}

typedef std::function<Base*()> creationFunction;

enum class_type { eD1, eD2, eD3, eD4, eD5 };

creationFunction create [] =
{
    new_class<D1>,
    new_class<D2>,
    new_class<D3>,
    new_class<D4>,
    new_class<D5>
};

Base* getClass(class_type type)
{
    return create[type]();
}

int main()
{
    std::mt19937 engine((std::random_device())());
    std::uniform_int_distribution<unsigned> ct(eD1, eD5);

    auto rand_class = [&]() { return static_cast<class_type>(ct(engine)); };

    std::vector<std::unique_ptr<Base>> container;
    for (unsigned i = 0; i < 20; ++i)
        container.emplace_back(getClass(rand_class()));

    for (auto & element : container)
        element->print();
}



Instead of updating a switch, you update the class_type enum and the create array of function objects.

http://ideone.com/aLOQN0
closed account (o1vk4iN6)
I guess there's no shorter way to create the dynamic class.


C++ isn't a dynamic language, the language doesn't provide a way to do it, either way it isn't *magic* for dynamic languages. They still need to find the class and such it's simply a matter of you implementing the method you want to do that.
Topic archived. No new replies allowed.