Is it OK to typedef a struct, say A, which contains an iterator of list<A>?

102 Views Asked by At

So my typedef is like:

typedef struct {
    B b;
    std::list<A>::iterator iter;
} A;

I know a self-referential pointer works, but not so sure about an iterator, although they are quite alike. Just want to make sure, thanks.

1

There are 1 best solutions below

0
On
typedef struct { /*...*/ } A;

is vestigial C syntax. The C++ way to write this is

struct A { /* ... */ };

As for storing an iterator, it's not really clear why you have any confusion about this.

However, using iterators requires caution. Many operations can invalidate existing operators, for example:

std::vector<int> v;
v.push_back(1);
v.push_back(2);
std::vector<int>::iterator it = v.begin() + 1; // references 2
v.insert(v.begin() + 1, 0); // invalidates it

storing iterators is a sure-fire way to lead into a world of this kind of hurt.