Is possible to get type a child class type from base class?

  abstract-class, c++, templates

I need something like this:

template <typename T>
class A
{
public:
    T commonData;
    TYPEOF_CHILD_CLASS *test(T data)
    {
        return new CHILD_CLASS(data);
    }
};

template <typename T>
class B : public A<T>
{
public:
    T bdata;
    B(T data)
    {
        bdata = data;
    }
};

template <typename T>
class C : public A<T>
{
public:
    T cdata;
    C(T data)
    {
        cdata = data;
    }
};

int main(int argc, char const *argv[])
{
    auto a = C<int>(1).test(2);
    return 0;
}

Is possible to define TYPEOF_CHILD_CLASS in c++? Before I used D. In D it works with:

R (this R) test(T data)
{
   return new R(data);
}
...

void main()
{
    auto a = new C!A(1).test(2);
}

It is a template. Does the c++ have a similar method? Maybe can I set type also via a template? Or may be I determine typename via typeid?

Source: Windows Questions C++

LEAVE A COMMENT