const使用大全

先看以下两个文章:

http://www.vckbase.com/document/viewdoc/?id=689

http://www.vckbase.com/document/viewdoc/?id=412

 

补充:

  1. #include <stdlib.h>     //system()
  2. #include <iostream>     //cout<<
  3. #include <memory>       //auto_ptr<>
  4. using namespace std;
  5. class MyClass
  6. {
  7. public:
  8.     int x;
  9.     MyClass()
  10.     {
  11.         //void constructor
  12.     }
  13.     ~MyClass()
  14.     {
  15.         //destructor
  16.     }
  17.     int MyFunc( int/*const*/ y )    
  18.     {
  19.         *y = 20;    // 改变外部变量的值 int MyFunc(const int  *y ) or int MyFunc( int const *y ) 时报错
  20.         x = *y ;    // 改变类内部变量值 int MyFunc( int  *y ) const 时报错
  21.         y = &x ;    // 改变传来的指针参数值 int MyFunc( int  *y ) const or int MyFunc( int* const y )  时报错
  22.         return 0;
  23.     }
  24.     int MyFunc2( int /*const*/ &y )    
  25.     {
  26.         y = 20;     // 改变外部变量的值 int MyFunc2(const int  &y ) or int MyFunc2( int const &y ) 时报错
  27.         x = y ;     // 改变类内部变量值 int MyFunc2( int  &y ) const 时报错
  28.         return 0;
  29.     }
  30. };
  31. //-----------------------------------------
  32. void Fun()
  33. {
  34.     //-----------------------------------------
  35.     MyClass m;
  36.     int z = 10;
  37.     m.MyFunc(&z);
  38.     printf("call MyFunc:  z=%d, MyClass.x=%d/t/n",z,m.x);   
  39.     m.MyFunc2(z);
  40.     printf("call MyFunc2:  z=%d, MyClass.x=%d/t/n",z,m.x);    
  41.     return;
  42.     //--------------------------------------------
  43. }
  44. void main()
  45. {
  46.     Fun();
  47.     system("pause");
  48. }
  49. //-----------------------------------------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值