Following up on this question, people have suggested I go with "option 3" which might look like this:
class b2Body {
private:
b2Body() {}
friend class b2World;
};
class Body : public b2Body {
private:
Body() {}
friend class World;
};
class b2World {
public:
b2Body *CreateBody() {
return new b2Body;
}
};
class World : public b2World {
public:
Body *CreateBody() {
return new Body;
}
};
int main() {
World w;
Body *b = w.CreateBody();
return 0;
}
But there are two major problems with this:
Bodycan never be constructed, even withWorld::CreateBody(becauseb2Bodyis private)- Even if it could, then the
b2Bodypart wouldn't be initialized correctly (b2World::CreateBodyneeds to be called)
Does this mean I can never inherit from b2Body/b2World and follow this same design pattern? (keep in mind that I can't edit the b2* classes)
If so, I suppose you guys would recommend I just keep b2World and b2Body as member variables instead?
I think it comes down to these two options now:
Only
b2Worldcan create ab2Bodyso this just doesn't go anywhere. These classes clearly aren't designed to be inherited from so yes, aggregate them instead. What about something along the lines of