The first and second cout of main works fine, but the other two don't. It's like when I create an a and put it in _b it disappears.
What I am doing wrong?
Thanks for the help.
// this line creates an unnamed temporary b object, creates an a in it, then throws the b away
c1.getb(0).create_a(3);
// similar to this:
{
b temporary = c1.getb(0);
temporary.create_a(3);
} // <- 'temporary' dies here
So note that you're modifying the temporary object and not the actual b contained within the c1 object. c1's b object remains unaltered.
If you want to do this, you'd need to return a reference, rather than a copy:
1 2 3
b& getb(int id){ // <- return a 'b&', not a 'b'
return _c[id];
}