<未完>再次研究指针和引用

来源:互联网 发布:修改硬件信息软件 编辑:程序博客网 时间:2024/05/16 01:59
#include <iostream>using namespace std;class apple{public:int id;public:apple(int id){   this->id = id;}void operator=(apple tmp){this->id = tmp.id;}};void f1(apple* app){   app = new apple(101);}void f2(apple& app){   apple* tmp = new apple(101);   app = *tmp;}void f3(apple* &app){   app = new apple(101);}void f4(apple app){   apple* tmp = new apple(101);   app = *tmp;}int main(){   apple* app = new apple(100);   apple app2 = *app;   //f1(&app2);   //f3(&app2);报错   //apple* c = &app2;   //f3(c);   f4(app2);   //app->id   cout << app2.id << endl;}//f1:100//f2:101//f3:101//f4:100//app2//f1:100//f2:101//f3:错误//f3:100//f4:100
#include <iostream>using namespace std;class apple{public:int id;public:apple(int id){   this->id = id;}void operator=(apple tmp){this->id = tmp.id;}};void f1(apple* app){   //app = new apple(101);app->id = 101;}void f2(apple& app){   apple* tmp = new apple(101);   app = *tmp;}void f3(apple* &app){   app = new apple(101);}void f4(apple app){   apple* tmp = new apple(101);   app = *tmp;}int main(){   apple* app = new apple(100);   apple app2 = *app;   //f1(&app2);   //f3(&app2);报错   //apple* c = &app2;   //f3(c);   //f4(app2);   //app->id   f1(app);   cout << app->id << endl;}//这个竟然输出101………………


原创粉丝点击