Notes
C Plus Plus
C Plus Plus
  • Roadmap
    • 🛣️C Plus Plus: Docs
  • Build
    • 🔥Build Process
    • 🔧Connect multiple C++ Files
  • Code
    • ⏮️Pre-Processors
      • #include
      • #define
      • #ifdef
      • #pragma
      • Predefined Macros
    • LValue and RValue
    • 🪅Data Types
      • Enum
      • TypeDef
      • const in c++
      • extern vs inline
    • 🎭Casting
    • 🔃Operator overloading
      • Available Operators
      • Examples of operator overloading
    • 🗺️Namespace
      • Namespace Example
      • Using directive
    • 🕵️Header File
      • For C++ Classes
    • 🏗️Structure
      • Struct vs Class
      • Public vs Private inheritance
    • 🏢Classes
      • Friend Function
      • Copy Constructor
      • Explicit Constructor
      • Move Constructor
        • Move Semantics
      • Other constructors
      • Virtual functions
      • Pure virtual function
      • Other function declaration
      • const function vs final function
  • Memory
    • 🧠Memory Introduction
    • ✨Heap and Stack
    • 🎯Pointers
      • Dangling Pointer
      • 'this' Pointer
      • Function Pointer
      • Smart Pointers
        • Unique Pointer
        • Shared Pointer
        • Weak Pointer
      • Reference count
    • 👨‍🏭Helper function
    • 🍡Vector [ArrayList]
      • Custom vector, part 1
      • Custom vector, part 2
      • Custom vector, part 3
      • std::vector
    • ♻️Union
      • Type Punning
      • Type Punning, part 2
      • Type Punning, part 3
      • Union, part 1
      • Union, Part 2
  • Thread
    • 🧵Threading
      • std::thread
      • Detach a thread
  • Misc
    • 🗂️Execution Order
    • 🧠Print memory
Powered by GitBook
On this page
  1. Memory
  2. Union

Union, Part 2

Now, we lets move to a little complicated example with struct inside union with multiple size:

Here is a example:

#include <iostream>

struct Vector1
{
    int a, b;
};

struct Vector2
{
    union
    {
        struct
        {
            int a,b,c,d;
        };
        struct
        {
            Vector1 x,y;
        };
    };
    
};

void printVector1(Vector1& v1) {
    std::cout << v1.a << ", " << v1.b << std::endl;
}

int main(int argc, char const *argv[])
{
    Vector2 v = {1,2,3,4};
    printVector1(v.x);
    printVector1(v.y);

    v.d = 500;
    printVector1(v.x);
    printVector1(v.y);

    return 0;
}

Output:

1, 2
3, 4
1, 2
3, 500

Lets explore this example:

  1. Here, we created 2 structs, Vector1 and Vector2.

  2. Vector1 contains two integers: a, b.

  3. Vector2 contains two possible values based on union, either it will have four integers a, b, c, d or it will contain 2 Vector1.

  4. Now, if we create an object of Vector2 with values {1, 2, 3, 4}.

  5. This Vector2 instance can be converted into two instances of Vector1 as it was part of the vector.

  6. And, if we update a value of Vector2 insrance and read it as Vector1 Instance, you will see that Vector1 data has been changes as they are referring to same memory location.

PreviousUnion, part 1NextThreading

Last updated 8 months ago

Try it :

♻️
here