Search code examples
c++structurememberstdlist

Is it possible to have a std::list as a member of a structure in C++?


I'll describe my question using an example. I have the structure Triangle:

struct Triangle {
int perimeter;
double area;
list <mystruct> sides;
};

where mystruct is:

struct mystruct {
int length;
}

Is it possible? Are there any problems that may appear?


Solution

  • Yes, it's possible. In fact it's a composition. You can use it like this:

    mystruct s;
    s.length = 10;
    
    Triangle t;
    t.sides.push_back(s);
    

    object composition is a way to combine simple objects or data types into more complex ones.