Can I handle a class as objects in C ++

Here is what I'm trying to achieve: I have a list of classes (Class1 to Classn) that inherit from the main class. I would like to be able to initialize an object of any of n classes without having to make a big switch case (or equivalent). sort of:

static ClassPointerType const * const ArrayOfClassTypes[]={ Class1, Class2, .. Classn }; 

static Class *GetObjectOfClass(int i)
{
  return new ArrayOfClassTypes[i](some parameters for the constructor);
}

You can do this in other OO languages ​​like Delphi, where you have a TClass type and can get an object class ... but I could not find equivalent functionality in C ++.

+4
source share
5 answers

Are you looking for something like this?

template<typename T>
std::unique_ptr<base> make()
{
    return std::unique_ptr<base>(new T);
}

class factory
{
    static constexpr std::unique_ptr<Base> (*fns[])(){make<derived_a>, make<derived_b>};

    std::unique_ptr<base> get_object_of_class(int const i)
    {
        if (i < 0 || sizeof fns / sizeof *fns <= i) {
            return nullptr;
        }

        return fns[i]();
    }
};
+9
source

Clone (= NULL). .
.
factory :

static ClassPointerType *GetObjectOfClass(int i)
{
  return new ArrayOfClassTypes[i]->Clone(some params for the constructor);
}

, :

static ClassPointerType* const ArrayOfClassTypes[]={ new ClassPointerType1(),new ClassPointerType2(), .. new ClassPointerTypeN() }; 

:

// base class
class ClassPointerType
{
public:
    virtual ClassPointerType* Clone(your params) = NULL;
};

// concrete classes
class ClassPointerType1 : public ClassPointerType
{
    public:
        // note: it clones own concrete instance
        virtual ClassPointerType* Clone(your params) {return new ClassPointerType1(your params)};
}

class ClassPointerType2 : public ClassPointerType
{
    public:
        virtual ClassPointerType* Clone(your params) {return new ClassPointerType2(your params)};
}


class ClassPointerTypeN : public ClassPointerType
{
    public:
        virtual ClassPointerType* Clone(your params) {return new ClassPointerTypeN(your params)};
}
+1

- . - factory ,

,

a) factory

b) .

. :

int main(int argc, char* argv[])
{
    DerivedA::announce();

    //and later
    IInterface * prt = SingeltonFactory::create(DerivedA::_type);

    delete prt;

    return 0;
}

DerivedA :

class DerivedA :
      public IInterface,
      public StaticBase<DerivedA>
{
public:
    using StaticBase::announce;
    static IInterface * create(){ return new DerivedA; }
    static const std::string _type;

};
const std::string DerivedA::_type=std::string("DerivedA");

, _type create, :

template<class TDerived>
class StaticBase
{
protected:
    static void announce()
    {
        // register into factory:
        SingeltonFactory::registerFun(TDerived::_type,TDerived::_create());
        // The call of _type and _create implicitly forces the derived class to implement these, if it is deriving from this Base class
    }
};

factory

std::map<std::string,tFunPtr> 

typedef tFunPtr:

typedef IInterface * (*tFunPtr)(void);

" ", . , create

? factory?

0

, ++ 11, , , , :

#include <iostream>
#include <memory>
#include <array>

class Base {
public:
    virtual void doSomething() = 0;
};

class Der1: public Base {
private:
    void doSomething() override {
    std::cout << "Der1 did something" << std::endl;
}
};

class Der2: public Base {
private:
    void doSomething() override {
    std::cout << "Der2 did something" << std::endl;
}

};

template <typename T>
std::unique_ptr<Base> make() {
    return std::unique_ptr<T>(new T);
}

int main() {

    std::array<std::function<std::unique_ptr<Base>(void)>, 2> arr{make<Der1>,
                                                                  make<Der2>};

    auto obj = arr[0]();
    obj->doSomething();
    obj = arr[1]();
    obj->doSomething();
}

std:: bind ,

,

0

No, in C ++ classes are not first order objects.

(I just noticed that everyone offers a solution to your problem, but they don’t exactly answer your question.)

0
source

Source: https://habr.com/ru/post/1524662/


All Articles