Suppose I have an object with many members:
class Example {
AnotherClass member1;
AnotherClass member2;
YetAnotherClass member3;
...
};
Is there a short/concise way to do something like:
foreach(member m: myExample)
m.sharedMethod();
Instead of accessing each one individually?
I think I could put them in a vector and use a shared_ptr for the same effect, I was just wondering if say, Boost or some other popular library doesn’t have something to do this automatically.
C++ does not support class introspection, so you cannot iterate over all of the members in a class like that – not without having a (manually written) function that iterates over all members for you anyway.
You could, in principle, add a template member like so:
That said, I would regard this as a broken design; you’ve gone and exposed all of your internal members publicly. What happens if you add one later? Or change the semantics of one? Make one a cached value that’s sometimes out of date? etc. Moreover, what happens if you have members which don’t all inherit from the same types?
Step back and reconsider your design.