   [C++] Passing this pointer to static method
  I have a class which is calling a static method in another class but for some reason one of it's public pointers becomes NULL.Below is inside the constructor of the class, where CreateRope is a static method being called with a "this" pointer passed throughfprintf(stderr, "this:%d this->Body:%d\n", (int)this, (int)this->Body);Segment::CreateRope(Vector3DMake(0, 0, 0), SizeFMake(8 * SCALE, Len), 6, 0.2f, platform, this, g_textureActors, "rope");Now below is inside the static methodvoid Segment::CreateRope(Vector3D position, SizeF size, int segmentCount, int segmentGap, Actor* actorStart, Actor* actorEnd, Texture2D* texture, const string& skin){fprintf(stderr, "actorEnd:%d actorEnd->Body:%d\n", (int)actorEnd, (int)actorEnd->Body);...The output I get isthis:63311696 this->Body:76121696actorEnd:63311696 actorEnd->Body:0How can Body have an address before it's passed into the static method but is NULL inside the static method?[Edited by - Headkaze on April 22, 2010 12:37:24 AM]
  I don't know if this applies to your particular case, but in general using this inside a constructor is risky. Depending on how you've written the constructor, the class may not be fully initialized by the time you reference this, which could cause the program to explode in various and spectacular ways.It seems unlikely that this is your problem, though. Is there any code between fprintf and the call to Segment::CreateRope that you're omitting here?   ;  This just doesn't make sense. If I pass through this->Body as a parametervoid Segment::CreateRope(Vector3D position, SizeF size, int segmentCount, int segmentGap, Actor* actorStart, Actor* actorEnd, b2Body* body, Texture2D* texture, const string& skin){fprintf(stderr, "actorEnd:%d actorEnd->Body:%d body:%d\n", (int)actorEnd, (int)actorEnd->Body, (int)body);I get the following output:this:63115408 this->Body:77541472actorEnd:63115408 actorEnd->Body:0 body:77541472Can someone explain why I can't access the public variable "Body" using the pointer (actorEnd->Body)?;Quote:Original post by AristeI don't know if this applies to your particular case, but in general using this inside a constructor is risky. Depending on how you've written the constructor, the class may not be fully initialized by the time you reference this, which could cause the program to explode in various and spectacular ways.It seems unlikely that this is your problem, though. Is there any code between fprintf and the call to Segment::CreateRope that you're omitting here?I have also tried calling it from another method outside of the constructor and the same thing occurs.I do not have any code between fprintf and the call to Segment::CreateRope.   ;  Is this->Body still non-zero after the call to Segment::CreateRope? Can you show all the code for the constructor and the static function? It's difficult to tell what's going on with the snippets you've provided.   ;  That's pretty bizzare.Can you cut out enough code to post a compilable example program that exhibits this behaviour?   ;  Nevermind I just realised what my coding partner has done. Instead of using the superclass's "Body" variable he's defined another one in the subclass. The parameter "Actor*" is the superclass but the pointer passed through (this) is the subclass.   ;  Ah, that would do it :)   