S
S
Snelsi2018-11-27 22:15:31
C++ / C#
Snelsi, 2018-11-27 22:15:31

Is it possible in C++ to create a dynamic mimic class that can mimic another class more than once?

On the example of the game:
There are two classes "Vampire" and "Elf" with their own parameters and methods. And there is the Mimic class , which, when creating an object, does not have a rigid binding to some other class, but during the game it can temporarily turn into a character of another class and get its capabilities .
Is it possible to implement this with C++ classes? (Given that the implementation of all other classes can be tailored just for this purpose.) . Is it possible to make such a binding at least once during the lifetime of an object?
Options through crutches are also of interest.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Victor Vakhturov, 2018-11-27
@Snelsi

Use a strategy object or delegate objects.
The point is that the class implements its methods through similar delegate methods.
https://ru.wikipedia.org/wiki/%D0%A8%D0%B0%D0%B1%D...

S
sddvxd, 2018-11-27
@sddvxd

class MimicBase{};
    class Vampire : public MimicBase{
    public:
        Vampire();
        Vampire(MimicBase& m) : MimicBase(m){};
    };
    class Elf : public MimicBase{
    public:
        Elf();
        Elf(MimicBase& m) : MimicBase(m){};
    };

    MimicBase m1;
    Vampire v1(m1);
    MimicBase m2(v1); //Обратно

The bottom line is that when initializing a class object, for example, Vampire, you take control of the call of the constructor chain and can substitute the parent object

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question