Shouldn't b2World of b2Body be mutable?

Changing the world does not change the body, following code is invalid in the current version:

const b2Body * b;
b->GetWorld()->CreateBody(blah);

Is there a reason GetWorld() of const b2Body returns a const pointer?
By the looks of the API, there are both versions. Try dropping the const off your "b2Body *b"

Rick
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).
How are you using this?

Personally, I'll make something non const if any parents are modified. To me, const means no side effects except for possibly arguments passed by reference. (And stuff like debug logging of course).
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);

Original thread: https://box2d.org/forum/viewtopic.php?t=7249