Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- struct Component
- {
- enum types { a, b };
- // Common data such as id
- };
- struct Component_a : public Component
- {
- float f1;
- float f2;
- float f3;
- Component_a(float f1, float f2, float f3)
- {
- this->f1 = f1;
- this->f2 = f2;
- this->f3 = f3;
- }
- void print()
- {
- std::cout << "f1: " << f1 << ", f2: " << f2 << ", f3: " << f3 << std::endl;
- }
- };
- struct Component_b : public Component
- {
- int i;
- Component_b(int i)
- {
- this->i = i;
- }
- void print()
- {
- std::cout << "i: " << i << std::endl;
- }
- };
- struct ComponentUnion {
- union {
- Component_a a;
- Component_b b;
- };
- Component::types type;
- ComponentUnion(Component_a a)
- {
- this->a = a;
- this->type = Component::types::a;
- }
- ComponentUnion(Component_b b)
- {
- this->b = b;
- this->type = Component::types::b;
- }
- };
- int main()
- {
- std::vector<ComponentUnion> v;
- v.push_back(ComponentUnion( Component_a(0.1f, 0.2f, 0.3f) ));
- v.push_back(ComponentUnion( Component_b(1) ));
- for (ComponentUnion& element : v)
- {
- switch (element.type)
- {
- case Component::types::a:
- // Interpret as type Blueprinta
- element.a.print();
- break;
- case Component::types::b:
- // Interpret as type Blueprintb
- element.b.print();
- break;
- }
- }
- system("PAUSE");
- }
- /*
- Output:
- f1: 0.1, f2: 0.2, f3: 0.3
- i: 1
- */
Add Comment
Please, Sign In to add comment