On the parameter of C + + class member function

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.