关键词搜索

源码搜索 ×
×

const A& fun(const A& a) const {} 的理解

发布2014-11-08浏览12775次

详情内容

       废话少说, 直接上菜:

 

  1. #include <iostream>
  2. using namespace std;
  3. class A
  4. {
  5. public:
  6. int x;
  7. A(A&)
  8. {
  9. cout << "copy constructor" << endl;
  10. }
  11. void fun() const
  12. {
  13. x = 1; // error, 这个const限定该成员函数不能改变成员变量
  14. }
  15. };
  16. int main()
  17. {
  18. return 0;
  19. }


     继续:

 

 

  1. #include <iostream>
  2. using namespace std;
  3. class A
  4. {
  5. public:
  6. int x;
  7. A()
  8. {
  9. }
  10. A(A&)
  11. {
  12. cout << "copy constructor" << endl;
  13. }
  14. void fun(A a) // 有对象拷贝
  15. {
  16. }
  17. };
  18. int main()
  19. {
  20. A a, b;
  21. a.fun(b); // 会有拷贝构造函数被调用
  22. return 0;
  23. }


     继续:

 

 

  1. #include <iostream>
  2. using namespace std;
  3. class A
  4. {
  5. public:
  6. int x;
  7. A()
  8. {
  9. }
  10. A(A&)
  11. {
  12. cout << "copy constructor" << endl;
  13. }
  14. void fun(A& a) // 引用
  15. {
  16. }
  17. };
  18. int main()
  19. {
  20. A a, b;
  21. a.fun(b); // 没有拷贝构造函数被调用
  22. return 0;
  23. }


     继续看:

 

 

  1. #include <iostream>
  2. using namespace std;
  3. class A
  4. {
  5. public:
  6. int x;
  7. A()
  8. {
  9. }
  10. A(A&)
  11. {
  12. cout << "copy constructor" << endl;
  13. }
  14. void fun(const A& a)
  15. {
  16. a.x = 100; // error, const引用, 所以a.x的值不能改变
  17. }
  18. };
  19. int main()
  20. {
  21. A a, b;
  22. a.fun(b);
  23. return 0;
  24. }


     go on:

 

 

  1. #include <iostream>
  2. using namespace std;
  3. class A
  4. {
  5. public:
  6. int x;
  7. A()
  8. {
  9. }
  10. A(A&)
  11. {
  12. cout << "copy constructor" << endl;
  13. }
  14. A fun()
  15. {
  16. A aa;
  17. return aa;
  18. }
  19. };
  20. int main()
  21. {
  22. A a;
  23. a.fun(); // 会有拷贝构造函数被调用
  24. return 0;
  25. }

      

 

     go on:

 

  1. #include <iostream>
  2. using namespace std;
  3. class A
  4. {
  5. public:
  6. int x;
  7. A()
  8. {
  9. }
  10. A(A&)
  11. {
  12. cout << "copy constructor" << endl;
  13. }
  14. A& fun()
  15. {
  16. A aa;
  17. return aa; // danger, 返回局部对象的引用非常危险, 此时仅仅作示意用
  18. }
  19. };
  20. int main()
  21. {
  22. A a;
  23. a.fun(); // 没有拷贝构造函数被调用
  24. return 0;
  25. }

     go on:

  1. #include <iostream>
  2. using namespace std;
  3. class A
  4. {
  5. public:
  6. int x;
  7. A()
  8. {
  9. }
  10. A(A&)
  11. {
  12. cout << "copy constructor" << endl;
  13. }
  14. A& fun(A& a)
  15. {
  16. return a;
  17. }
  18. };
  19. int main()
  20. {
  21. A a, b;
  22. a.fun(b).x = 1; // ok
  23. return 0;
  24. }

    

 

      go on:

  1. #include <iostream>
  2. using namespace std;
  3. class A
  4. {
  5. public:
  6. int x;
  7. A()
  8. {
  9. }
  10. A(A&)
  11. {
  12. cout << "copy constructor" << endl;
  13. }
  14. const A& fun(A& a)
  15. {
  16. return a;
  17. }
  18. };
  19. int main()
  20. {
  21. A a, b;
  22. a.fun(b).x = 1; // error, 常引用
  23. return 0;
  24. }


    go on:

 

 

  1. #include <iostream>
  2. using namespace std;
  3. class A
  4. {
  5. public:
  6. int x;
  7. A()
  8. {
  9. }
  10. A(A&)
  11. {
  12. cout << "copy constructor" << endl;
  13. }
  14. A& fun(const A& a)
  15. {
  16. return a;
  17. }
  18. };
  19. int main()
  20. {
  21. A a, b;
  22. a.fun(b).x = 1; // error
  23. return 0;
  24. }


      最后的菜:

 

 

  1. #include <iostream>
  2. using namespace std;
  3. class A
  4. {
  5. public:
  6. int x;
  7. A()
  8. {
  9. }
  10. A(A&)
  11. {
  12. cout << "copy constructor" << endl;
  13. }
  14. const A& fun(const A& a) const
  15. {
  16. return *this;
  17. }
  18. };
  19. // ok
  20. int main()
  21. {
  22. A a, b;
  23. a.fun(b);
  24. return 0;
  25. }

 

 

 

 

 

 

相关技术文章

最新源码

下载排行榜

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载