|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 小咒 于 2015-3-19 13:14 编辑
this指针所指向的是对象本身?
我的想法是这样的,我设置一个函数的返回值是一个对象,随后用一个对象去调用这个函数(函数中用this来返回这个对象),把返回的值赋给另一个对象,程序在这里,但是为什么是错误的呢,求解答。(目前还是对this的概念很混淆。。迷糊中,谢谢各位!)
- #include <iostream>
- using namespace std;
- class A
- {
- public:
- A(){i=new int;*i=NULL;}
- A(int x){i=new int;*i=x;}
- //A(const A&s){i=new int;*i=s.print();}
- ~A(){delete i;*i=NULL;}
- int print(){return *i;}
- A &printthis()//错误
- {
- return *this;
- }
- private:
- int *i;
- };
- void main()
- {
- A a(123),b(456),c(789),d;
- cout<<a.print()<<endl;
- cout<<b.print()<<endl;
- cout<<c.print()<<endl;
- cout<<"*********************\n";
- d=a.printthis();//错误
- }
复制代码
|
|