-5

I am quite new to c++. I read that structs in c++ are created at compile time, so there is probably no way to do dynamical structs. But maybe there is another container type, which fits my situation? I want to parse data from HDF5 files and use them in C++. So I don't know the content at compile time.

Basically I have 3 types (let it be A,B,C) of objects which I want to be able to store in a struct like manner - that means accessing it by the name, which is provided by HDF5 file. However I don't know at compile time the number of objects with type A,B or C. But when parsing the HDF5 file I can find out. Is there any container in c++ which supports that or do I have to write my own?

Gurmanjot Singh
  • 10,224
  • 2
  • 19
  • 43
v.tralala
  • 1,444
  • 3
  • 18
  • 39
  • if you really only have three types (`A`,`B`,`C`) then you are overcomplicating the problem. Simply use a struct that contains three vectors of `A`,`B` and `C` respectively – 463035818_is_not_an_ai May 13 '17 at 14:08
  • 1
    All your questions [are going to be answered here](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). If you're "quite new to C++", you should be spending your time learning it, instead of attempting to implement a moderately-complicated task without having a good grasp on the fundamentals of the language. Nothing you've described sounds anything more than a moderately complicated task, but you need to know C++ first. Go and pick some C++ book, first, then start reading it. – Sam Varshavchik May 13 '17 at 14:08
  • "*I read that structs in c++ are created at compile time, so there is probably no way to do dynamical structs*" I don't think that statement even means anything in C++. – juanchopanza May 13 '17 at 14:13
  • @SamVarshavchik yes I also visited this question and I am reading, but also working paralelly since time is short – v.tralala May 13 '17 at 14:17
  • @tobi303 how would I then be able to access the object of type A per name? – v.tralala May 13 '17 at 14:18
  • @juanchopanza what I mean was written here: http://stackoverflow.com/a/18673346/5105118 – v.tralala May 13 '17 at 14:21
  • 2
    The proper way to learn C++ is not to get a job writing C++ code without first knowing the language, and then trying to figure out as you go along. C++ is, arguably, one of the most complicated contemporary general purpose programming languages. "Learning on the job" does not work with C++. The answer to your question is "use a map keyed by object name, containing a `shared_ptr` to a base class, with all of your three classes virtually derived from it". That's how this is done in C++. If you don't understand it, it can't be explained in a paragraph, or two. Sorry. Go read a book. – Sam Varshavchik May 13 '17 at 14:26
  • @SamVarshavchik well thats arguable and surely depends on which task to do in C++. But I agree that C++ needs to be learned from the ground. And your last anwser was the thing I was looking for. Thank you. – v.tralala May 13 '17 at 14:39

1 Answers1

4

For accessing data by a string you can use std::map<std::string, T>. Now the next question is what is type T? As you know the types you want to deal with at compile time, a std::variant(or std::any) is the right and typesafe container. If you don't have C++17 at your disposal, take a look at boost::variant. They are basically tagged unions. A simple non generic implementation for types X, Y could look like this:

struct XY {
    union {
       X x;
       Y y;
     };
    bool isX;
    XY(X const& x) :x{x}, isX{true} {}
    XY(Y const& y) :y{y}, isX{false} {}
};

With this data-structure you are able to check which type is stored and retrieve it. Combining with std::map to std::map<std::string, XY> enables you to access it by it's name.

Gaetano
  • 1,090
  • 1
  • 9
  • 25