C++ How to create a heterogeneous container

前端 未结 4 1747
攒了一身酷
攒了一身酷 2021-01-01 01:25

I need to store a series of data-points in the form of (name, value), where the value could take different types.

I am trying to use a class template for each data-

4条回答
  •  执笔经年
    2021-01-01 01:42

    The problem with containers like this is that when you want to access something in the container, you have to determine its type and then cast it to the actual type somehow. This is ugly, inefficient and error-prone, which is why the #1 choice in C++ is to use inheritance, unless you have a very good reason not to - something I've never actually come across in my C++ career.

提交回复
热议问题