通过传址来传递和返回对象

来源:互联网 发布:lol代升级软件 编辑:程序博客网 时间:2024/06/03 17:42


#include <iostream>#include <string>using namespace std;class Test{private:string name;public:void setName(string& name){this->name=name;}string& getName(){return name;}};int main(void) {Test t;string s1="depp";t.setName(s1);cout<<t.getName();return 0;}


如果不用传地址的方式,直接传值,则可直接

t.setName("depp");

当然方法也需要进行修改,把“&”取地址符号去掉

0 0