1

let's say I have the following :

class A {

B b;
C c;

};

//....

A array[20];

Now I want to have access to the "b" member of class A, as if it was in an array. Is there a C/C++ way to do this ? For instance with the knowledge of just the pointer to array and the size (20), I would like to be able to enumerate only the "b" members, like new_array[i]. Is there a way to do this without directly referencing the class A ?

edit : reason I want to do this : I have a library which is storing this A array, and I'd like to only expose the class B (not A) to the users of the lib. I'd also like for the users of the library to iterate through the "b" members of "array", without having them know that it's actually a "b" member of a class A.

lezebulon
  • 7,607
  • 11
  • 42
  • 73
  • Can you provide an example? I don't understand the question. Are you asking how to iterate through the elements in `array` and access each of their `b` members? – Cameron Apr 01 '15 at 20:05
  • 1
    If b is made public then you can use `array[i].b` – NathanOliver Apr 01 '15 at 20:07
  • @Cameron : basically I have a library which is storing this A array, and I'd like to only expose the class B (not A). I'd also like for the users of the library to iterate through the "b" members of "array", without having them know that it's actually a "b" member of a class A. – lezebulon Apr 01 '15 at 20:08
  • 7
    You want to use a custom iterator, an [example question about making one](http://stackoverflow.com/questions/3582608/how-to-correctly-implement-custom-iterators-and-const-iterators). – Guvante Apr 01 '15 at 20:12

0 Answers0