mardi 30 juillet 2019

Easy way to create object with members of various datatypes

In C++, I often need to use an extremely simple, primative object containing a variety of datatypes simply so that I can pass it around easily between functions. In Python, I achieve this by using dictionaries. For example:

easy = {"keyframe_range":[], "value":0.25, "interpolation":"bezier"}

However, in C++, if I wanted to create something similar, I would need to:

struct obj
{
  vector<int> keyframe_range;
  float value;
  string interpolation;

  obj(vector<int> vv, float ff, string ss) : keyframe_range(vv), value(ff), interpolation(ss) {}

};

obj easy(vector<int>(), 0.25, "bezier");

When I need to create who-knows what kind of object on a whim, It's extremely inefficient and a huge waste of time to manually write a parameterized constructor for every object I need. In Python, I can essentially avoid this with dictionaries; however, unordered_maps in C++ must map to the same datatype, so they aren't really what I'm looking for.

Essentially, I'm just wanting an easy way to create a simple object which acts as a collection of items of various datatypes and nothing more. Can this be done in C++ 11, and if so, how?

Aucun commentaire:

Enregistrer un commentaire