0

I have a std::vector which contains some objects. How can I use memory addresses (do I want to use the this pointer for this purpose?) to check whether I am doing something with the same object?

For example:

void particle::calcFrc(std::vector<particle>& particles)
{
    vector3d tRel;
    mFrc.reset();
    for(unsigned int j = 0; j < particles.size(); j ++){

        if(... what goes here? ...){

            tRel = particles.at(j).mPos - mPos;

            if(tRel != zero()){
                // do stuff
            }
        }
    }
}

I want to do a check in the if statement to see whether particles.at(j) is referring to the same object from which this method was called.

Sam
  • 7,252
  • 16
  • 46
  • 65
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225

1 Answers1

0
if(... what goes here? ...){

I think what you are looking for is:

if (this != &particles[j]) {

... and the test will work fine, assuming that this object actually is one of the objects in the vector that was passed in to the method (and not just a temporary copy of one of them).

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234