简体   繁体   中英

Type casting problems with template class

I've following code and i want to store MyClass<int>,MyClass<float> ... in a vector<BaseClass> mItems then recover the original class.

I'm trying to figure out if there is a way in C++ to cast from base class to templated class, but still now i can not find the way how to do it.

MyClass<int> lRecover1 = dynamic_cast<MyClass<int>>(mItems.at(0));

This doesn't compile, and I don't understand why. Here is my code if it helps:

#include <vector>

class BaseClass
{
public:

    virtual ~BaseClass() {}
};


template<typename T>
class MyClass : public BaseClass
{
public:
    MyClass()
    {

    }

    ~MyClass()
    {

    }

};

void main()
{
    std::vector<BaseClass> mItems;

    MyClass<int> lItem1, lItem2;
    MyClass<float> lItem3, lItem4, lItem5;
    MyClass<double> lItem6, lItem7;


    //PROBLEM: I would like to recover the original class
    MyClass<int> lRecover1 = dynamic_cast<MyClass<int>>(mItems.at(0));
}

A vector of BaseClass instances will only contain actual instances of BaseClass by value. A vector of BaseClass* instances will contain pointers to BaseClass or any of its children.

std::vector<BaseClass*> items;
...
MyClass<int>* recover = dynamic_cast<MyClass<int>*>(items.at(0));

When you're dealing with the possibility of subclasses, you must always have a layer of indirection using either pointers or references. (And you have to use pointers in this case since references cannot be stored in an std::vector )

In particular, if you were to try to store MyClass<int> instances in a container of type std::vector<BaseClass> , C++ would try to copy the instances into the vector, using BaseClass 's copy constructor. This would lead to BaseClass copying the data that it knows about and failing to copy the child class information, effectively losing the extra data, a problem commonly referred to as object slicing .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM