Nested lists of classes

How can I create a nested list of different classes?

For example, I have a Figure list, declared like this:

list<Figure> ls1;

And I need to create a Vertex list and a Face list, both of them are part of the Figure class. How can I declare a list like that?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <list>

struct vertex { /* ... */ };
struct face { /* ... */ };

struct figure { vertex its_vertex ; face its_face ; /* ... */ };

int main()
{
    std::list<figure> seq(6) ; // sequence of six figures

    //////////////////  simple, recommended ///////////////////
    // iterate through the vertex sub objects
    for( const figure& f : seq )
    {
        const vertex& v = f.its_vertex ;
        // do something with v eg.
        std::cout << std::addressof(v) << ' ' ;
    }
    std::cout << '\n' ;

    //////////////////  slightly messy ///////////////////
    // create a sequence of (pointers to) face objects
    // note: using raw pointers here is perfect: these are 'non-owning' pointers
    std::list< face* > face_pointers ;
    for( figure& f : seq ) face_pointers.push_back( std::addressof( f.its_face ) ) ;

    // iterate through the face sub objects
    for( face* p : face_pointers ) { /* ... */ }

    // note: when the sequence of figures is modified, also update face_pointers
    // that is why this is 'slightly messy'
    seq.pop_back() ; face_pointers.pop_back() ;
    figure a_figure ;
    seq.push_front(a_figure) ; face_pointers.push_front( std::addressof( seq.front().its_face ) ) ;
}
Topic archived. No new replies allowed.