Jasu_M wrote:
Yes, I know there are both versions. However, I don't want to cast away constness. I can make some other work around but I'm not sure if "parent of const is const" is a good practice or not. There seems to be two camps though (getWindow() const returns non-const in Qt, the equivalent returns const in GTKmm).
C++ is not my strong suit by far, but i'm pretty sure, that const objects (in your case b) can only call const methods. For your question, the compiler doesn't know that GetWorld() won't change the body, the only way for it to be sure is to have a const version of GetWorld().
b2Body's have a pointer to their world, and the b2World has a link list of b2Body's.
Let's break down what you want to do:
Code:
const b2Body *b;
b2World *world = b->GetWorld();
world->CreateBody(newBody);
CreateBody(newBody) would update (what you want to be a mutable) world. This would update the world's m_bodyList member variable, thus mutating world, which is pointed to by b, which you've declared const.
Now, I'm going to be honest, I don't know, if that is strictly forbidden in C++, but it seems like a bad idea, and I believe this is why it is not allowed, The compiler doesn't know for a fact that CreateBody() won't modify b unless GetWorld() returns a const.
Am I making any sense?
Is there a reason you can't do this?
Code:
b2Body *b;
b->GetWorld()->CreateBody(blah);