Jump to content

  • Log In with Google      Sign In   
  • Create Account

Is it a very bad idea for a class to pass (*this) into a function? Strange bug...

  • You cannot reply to this topic
4 replies to this topic

#1 Sean_Seanston   Members   -  Reputation: 417

Like
0Likes
Like

Posted Today, 08:18 AM

Just wondering if it's a known source of errors for a class to do something like:

void ClassA::functionA()
{
     variableX = functionB( classA1, (*this) );
}

???

 

Because I was experiencing a very strange error last night when trying something like that, and it turned out the problem went away if I stopped passed (*this) and instead e.g. created another object of type ClassA that copied the values of (*this) and passed that in instead.

 

I admit it does seem like a strange, probably sloppy, construction, but I've never heard of it being something to avoid and if it was so specifically bad I would've expected some form of compiler error/warning either when compiling or running/debugging. The problem definitely seems to come from solely passing (*this), even when the function does nothing.

The bug I get then is quite puzzling to me, because it seems to be affecting things that are almost unrelated... the only thing I can figure is that an object passing itself like this might be somehow able to cause some kind of screw up/corruption with that object's data members in some circumstances. At first I thought maybe some kind of infinite loop but the program is still running.

 

So is this known to cause unexpected behaviour and to be avoided like the plague, or should I get into explaining my code quite specifically?

 

Also, I know that "this" is a pointer... and since * is used to dereference a pointer, does that mean passing (*this) is pass-by-value? That was my assumption at least, and hence I can't figure out why it would be a problem and passing in an object with the same values isn't. Unless it's some kind of pass-by-reference?



Sponsor:

#2 Glass_Knife   Moderators   -  Reputation: 5404

Like
0Likes
Like

Posted Today, 08:45 AM

There is something else going on.  C++ programs use *this all the time.

Vector2d& Vector2d::operator= (const Vector2d& param)
{
  x=param.x;
  y=param.y;
  return *this;
}

There should be no problem pass a (this*) to a function.  

 

 


Also, I know that "this" is a pointer... and since * is used to dereference a pointer, does that mean passing (*this) is pass-by-value?

 

As far as this part goes, it can be confusing.  C++ function arguments are always "pass-by-value".  Everything that goes into a function is copied.  That's why you pass a pointer or reference instead of an object.  It can be a lot cheaper to copy a 32bit pointer than a huge object.  For instance, when you pass a pointer and then access the thing it points to, you are using a copy of the pointer that points to the same object.

 

As an example, what does this do?

void func( int* intPointer ) {
   intPointer = new int{2};
}

void test() {
   int x = 4;
   int* intPointer = &x;
   func( intPointer );
   std::count << *intPointer << std::endl;
}

If the int* was passed by reference, then we have changed it to point to a different value.  Is that what happens?


Edited by Glass_Knife, Today, 02:21 PM.

I think, therefore I am. I think? - "George Carlin"
Indie Game Programming

#3 Mussi   Crossbones+   -  Reputation: 2133

Like
0Likes
Like

Posted Today, 08:53 AM

There's nothing wrong with dereferencing the this pointer, you're error has most likely something to do with your code(I'm guessing the second parameter is of type ClassA&).

 

 

 


Also, I know that "this" is a pointer... and since * is used to dereference a pointer, does that mean passing (*this) is pass-by-value?

That depends on the argument type in functionB, it could be by value, reference, const reference etc.


Edited by Mussi, Today, 08:53 AM.


#4 NightCreature83   Crossbones+   -  Reputation: 3099

Like
1Likes
Like

Posted Today, 11:16 AM

There is something else going on.  C++ programs use *this all the time.

Vector2d& Vector2d::operator= (const Vector2d& param)
{
  x=param.x;
  y=param.y;
  return *this;
}

There should be no problem pass a (this*) to a function.  

 

 


Also, I know that "this" is a pointer... and since * is used to dereference a pointer, does that mean passing (*this) is pass-by-value?

 

As far as this part goes, it can be confusing.  C++ function arguments are always "pass-by-value".  Everything that goes into a function is copied.  That's why you pass a pointer or reference instead of an object.  It can be a lot cheaper to copy a 32bit pointer than a huge object.  For instance, when you pass a pointer and then access the thing it points to, you are using a copy of the pointer that points to the same object.

 

As an example, what does this do?

void func( int* intPointer ) {
   intPointer = new int{2};
}

void test() {
   int x = 4;
   int* intPointer = &x;
   func( intPointer );
   std::count << *intPointer << std::endl;
}

If the int* was passed by reference, then we have changed it to point to a different value.  Is that what happens?

Just as a point but a pointer is not necessarily 32 bits and you should never ever write code that actually relies on the exact size of a pointer. This is what causes a lot of bugs in software when switching from 32-bit to 64-bit.

 


Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#5 Glass_Knife   Moderators   -  Reputation: 5404

Like
0Likes
Like

Posted Today, 02:21 PM


Just as a point but a pointer is not necessarily 32 bits and you should never ever write code that actually relies on the exact size of a pointer. This is what causes a lot of bugs in software when switching from 32-bit to 64-bit.

 

Yes, you're right.  Good point.


I think, therefore I am. I think? - "George Carlin"
Indie Game Programming





PARTNERS