I have classes A, B, C, D and the following hierarchy:
class A {
B* b;
};
class B {
C<D>* c;
};
template <class T>
class C<T> {
list<T*> ds;
};
class D {
};
I allocate A first as an A* member inside a base class, using new A(). Within A I allocate b using new B(). Within B I allocate c using new C<D>().
As soon as I attempt to call ds.size() the program crashes. An EXC_BAD_ACCESS arises inside ds when it tries to call its begin() method within size().
Do you know why this would happen? I’ve been using C++ for a few years but this is my first foray into using pointers within standard containers, so maybe I’m missing something obvious.
Your code, to my knowledge, should not care one way or another what the contents of list ds are. When you insert elements into containers and things like that you need to ensure that your element type supports all the necessary functionality (i.e. its assignable, copyable, and has the correct functions within it depending on which are required within that container class’ members).
The .size function should just return the value of an internal member though, it doesn’t make much sense for that to be the thing that is crashing. Are you sure everything was properly allocated in your real code? it sounds to me like you just have an uninitialied pointer somewhere in the call-chain.