J0hnD0e J0hnD0e - 3 months ago 9
C++ Question

Design pattern, a vector of objects, each of two possible types

I need to store objects of exactly two types in a vector, the both types have almost nothing in common.

After storing them in the vector, I want to iterate over that vector and perform an action, depending on the type.

my thoughts so far:


  • Polymorphism. Overkill, and wouldn't help me much, as I probably would do this:

    if(dynamic_cast<T1>() != nullptr) {
    ...
    } else {
    ...
    }

  • Merge both types (methods and fields) and add a boolean, representing if its type 1 or 2.



Both patterns seem totally clumsy to me, there is propably a total simple solution, I simply don't see.

The first type is something like this :

struct PatternMatch {
int length;
int indexInDict;
}


The second
one:

struct NoMatch {
std::string rawChars;
}

Answer

If you know that you have only two types and this number won't grow in future, a C-ish tagged union is enough and easy to use:

struct PatternMatch {
  int length;
  int indexInDict;
};

struct NoMatch {
  std::string rawChars;
};

struct TaggedUnion {
  enum { MATCH, NO_MATCH } flag;
  union {
    PatternMatch match;
    NoMatch noMatch;
  };
};

Now, you can create a vector of TaggedUnions and check the flag data member to find out the actual type of each element.

Comments