c++引用对象使用实例

以下将为大家介绍c++引用对象使用实例,值得注意的是定义引用时一定要同时对该引用进行初始化。因为引用就是一个常量。

#include <iostream>
using namespace std;

class Human
{
public :
int get(){return i;}
void set(int y){i=y;}
private:
int i;

};

int main()
{
Human xiaoMing;
Human &m=xiaoMing;
m.set(888);//c++通过引用调用set方法
cout<<m.get()<<endl;//通过引用调用方法get获取i的值
return 0;
} 本文链接地址: c++引用对象使用实例