类的引用类型成员

  1. From http://blog.csdn.net/hello_shadow/article/details/7007383
  2. #include <iostream>  
  3. using namespace std;  
  4. class Object  
  5. {  
  6.     int &count;  
  7.     public:  
  8.         Object(int &cou):count(cou){};  
  9.         //如果函数为Object(int cou)则count引用了形参。  
  10.         ~Object(){};  
  11.         void SetValue(int cou)  
  12.         {  
  13.             count=cou;  
  14.         }  
  15.         void Display()  
  16.         {  
  17.             cout<<"the value of count in Object:"<<count<<endl;  
  18.             cout<<"the address of count in main:"<<&count<<endl;  
  19.         }  
  20. };  
  21.   
  22. int main()  
  23. {  
  24.     int count=10;;  
  25.     Object obj(count);  
  26.     obj.SetValue(9);  
  27.     obj.Display();  
  28.     cout<<"the value of count in main:"<<count<<endl;  
  29.     cout<<"the address of count in main:"<<&count<<endl;  
  30.     return 0;  
  31. }  
posted on 2013-10-23 17:22  码哥@杭州  阅读(274)  评论(0编辑  收藏  举报