The recent code often appears, member function parameters will often occur in the value of the case, is also a relatively low-level error, here to tidy up for your reference.
(1) The member variable name is the same as the formal parameter name
World::world (unsigned maxcontacts, unsigned iterators) { resolver = iterators; Maxcontacts = maxcontacts; contacts = new Particlecontact[maxcontacts]; Calculateiterations = (iterators = = 0); }
Take a look at the 4th line of code here. The intention here is to pass the parameter of the member function to the member variable, the name of the two names. This does not have a syntax error, but it can cause parameters to be passed to member variables. Here's how to modify it:
World::world (unsigned maxcontacts, unsigned iterators) { resolver = iterators; World::maxcontacts = maxcontacts; contacts = new Particlecontact[maxcontacts]; Calculateiterations = (iterators = = 0); }
In addition, there is another way to do this:
World::world (unsigned maxcontacts, unsigned iterators) { resolver = iterators; This->maxcontacts = maxcontacts; contacts = new Particlecontact[maxcontacts]; Calculateiterations = (iterators = = 0);
}
(2) Class instance assignment to Pointer
void particle::setparticle (particle particle) { /*m_particle as pointer */ m_particle = &particle;}
This is because the formal parameter particle is a local variable, even if m_paritcle has pointed to particle, but after running this function, m_particle points to an empty address. So, one scenario is to add a reference, and the other is to pass the pointer directly, and the code is as follows:
void Particle::setparticle (particle& particle) { /*m_particle as pointer */ m_particle = &particle;}
void Particle::setparticle (particle* particle) { /*m_particle as pointer */ m_particle = &particle;}
for the time being so much, later encountered continue to add.
Arguments for C + + class member functions