&i Considerations for int i and int in C + +

Source: Internet
Author: User

Source: http://blog.csdn.net/qianchenglenger/article/details/16949689

1.int I-value, int & I-pass reference

int I does not return parameters, and int &i can be returned with parameters, such as

[CPP]View PlainCopy 
  1. #include <iostream>
  2. void test1 (int i)
  3. {
  4. i = 7;
  5. }
  6. void Test2 (int &i) //To limit parameter changes, you can add a const limit
  7. {
  8. i = 7;
  9. }
  10. int main ()
  11. {
  12. int t1 = 10;
  13. Test1 (t1);
  14. Std::cout << T1 << Std::endl; //output is ten
  15. int t2 = 10;
  16. Test2 (T2);
  17. Std::cout << T2 << Std::endl; //output is 7
  18. return 0;
  19. }
2. int I can assign constants, and int & I cannot

[CPP]View PlainCopy 
  1. #include <iostream>
  2. void test1 (int i)
  3. {
  4. i = 7;
  5. }
  6. void Test2 (int &i)
  7. {
  8. i = 7;
  9. }
  10. int main ()
  11. {
  12. int i = 10; //Legal
  13. int &i1 = 10; //Compilation error
  14. Test1 (10); //Legal
  15. Test2 (10); //Compilation error
  16. return 0;
  17. }
3. int &i is equivalent to an alias, and int i is just a copy

[CPP]View PlainCopy 
  1. #include <iostream>
  2. int main ()
  3. {
  4. int t1 = 10;
  5. int t2 = 10;
  6. int i1 = t1; //Copy
  7. int &i2 = t2; //Aliases
  8. I1 = 7;
  9. I2 = 7;
  10. Std::cout << T1 << Std::endl; //Output ten
  11. Std::cout << T2 << Std::endl; //Output 7
  12. return 0;
  13. }

Finally, let's take a look at another example

[CPP]View PlainCopy 
  1. #include <iostream>
  2. Class a{
  3. Public
  4. A (int a, int b): I1 (a), i2 (b) {};
  5. Public
  6. int i1;
  7. int &i2;
  8. };
  9. int main ()
  10. {
  11. A (45,60);
  12. Std::cout << a.i1 << "<< a.i2 << Std::endl;
  13. return 0;
  14. }

After running on the computer, you will find that the first number is normal, and the second number is obviously an undefined value, for example, I get the result after running

45 1400458944

This is because when we construct an object, we call the constructor, and the argument to the constructor of a is passed as a value, so when called, it is equivalent to having a

[CPP]View PlainCopy  
    1. int B = 60
I2 (b) is the equivalent of [CPP]View PlainCopy 
    1. int &i2 = b;
When the constructor call is complete, the scope of B is ended, B is destroyed, and i2 points to a place that has been destroyed, an undefined run result appears.
We're going to post a procedure that corresponds to the previous paragraph, but this time we'll get 45 60 results.
[CPP]View PlainCopy 
  1. #include <iostream>
  2. Class a{
  3. Public
  4. A (int a, int &b): I1 (a), i2 (b) {};
  5. Public
  6. int i1;
  7. int &i2;
  8. };
  9. int main ()
  10. {
  11. int t = 60;
  12. A (45,t);
  13. Std::cout << a.i1 << "<< a.i2 << Std::endl;
  14. return 0;
  15. }

&i Considerations for int i and int in C + +

Related Article

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.