How do I get the template type of a given element at runtime in C++?

前端 未结 3 949
情话喂你
情话喂你 2021-01-18 13:26

I\'m designing a simple Array class with the capability of holding any type of object, like a vector that can hold multiple types of data in one object. (This i

3条回答
  •  终归单人心
    2021-01-18 13:59

    The return type of a function like Array::get_element must be determined at compile time. Since class Array does not know what types of containers it stores at compile time, the only option is to just return the base Container* pointer. Then users of that code can use virtual functions and/or dynamic_cast to deal with those generalized containers.

提交回复
热议问题