I need to modify a really long program that was written by another programmer. Going through his code, you can see double pointers to C++ objects. I don’t understand why double pointers are used in this case, I think a single pointer would do the same thing. An example makes it more clear:
class A {
...
public:
static B** b; //why double pointers here?
...
}
Class B {
...
public:
B(...)
func1();
func2();
}
We need to have a number of objects from B class, let say 5 objects (not a 2D array of objects). Once we create b, the code never tampers with *b. Only B’s functions are called by b[i]->func1(). So, since we don’t change pointers, I would guess we could do the same thing by defining static B* b;. Am I neglecting something?
Maybe the programmer needed an array of objects of type B or deriving from B (or simply not prevent that capability in future programs)? Because the objects deriving from B can be of different size, you cannot simply put them one after another in an array – hence the additional level of reference.
In C++, if you are using boost library, this behavior can be relatively cleanly achieved by
boost::ptr_vectororboost::ptr_arraywhich will hide the uglyness of double pointers and prevent you from doing other accidental errors.