看板Programming
标 题Re: 请教c++的this传回值
发信站政大狂狷年少 (Sun Nov 12 02:19:21 2006)
转信站ptt!ctu-reader!ctu-peer!news.nctu!netnews.csie.nctu!news.cs.nthu!WHSHS
※ 引述《[email protected] ( )》之铭言:
> 因不知该如何显示出来,所以在这里询问,麻烦大家帮忙
> c++可以有this与*this,而我现在想了解这两种的传回值
> 不知该怎麽写
this 只是一个 pointer,
pointer 只是一种 variable,
所以根本就没有传回值这种说法,
顶多只能说内容值,
运算式的结果就单纯只是运算结果,
除非在谈 operator overloading,
不然几乎不会有人用运算式的传回值这种怪异说法,
事实上就算在谈 operator overloading,
那也不过是在讲运算子的传回值,
而不是讲运算式的传回值,
即使运算子的传回值恰好为运算式的结果亦然。
this 只会出现在 object 的 non-static method 里面,
是哪个 object 正在执行那个 method,
那 this 就指向哪个 object,
换句话说 this 的内容值就是该 object 的 start address。
this 和 *this 是两种不同的运算式,
并不是什麽可以有 this 与 *this 这样子讲,
在 C 和 C++ 把 * 这个称作 dereference operator 的运算子,
放在一个 pointer 前面构成一个运算式,
是非常理所当然的,
这是因为它是一元运算子;
而它的运算结果当然就是它指向的物件本身。
不管是发问或是和人交流讨论,
如果基本术语上的用词不能严谨,
这样会有很多人看不懂你在问什麽,
看得懂的也会因为你用词怪异,
觉得你连基本名词都搞不清楚(可能是连书都没好好看过就开始写程式),
那教了也有很大的可能是白教,
所以就不想回你的问题。
> 附上这两个程式码,麻烦各位点破我的迷思,谢谢
> #include <iostream>
> #include <cstdlib>
> using namespace std;
> class Cal
> {
> int x;
> public:
> Cal(int n) { x = n; } //建立者函数
> Cal calcSum(Cal obj) { //加法运算
> x += obj.x; //x = x + obj.x
看你搞不太懂 this 的意思,
所以顺带跟你说一下上面这行叙述等价於:
this->x += obj.x;
和
(*this).x += obj.x;
假设你正执行到 main() 中的 c = a.calcSum(b),
那麽 this 指向的 object 就是 a,
而 *this 就是 a 本身。
> return *this; //传回(*this).x
从注解可以看出你的观念有问题,
你明明写 return *this; 为什麽会认为是传回了 (*this).x 呢?
return *this 很显然就是传回 *this 的意思不是吗?
或是说,
你下这注解的意思是希望改成能达到传回 (*this).x 的值这种目的?
其实这也很简单,
你把 calcSum() 这个 method 的传回值型别改成跟 Cal::x 相同不就好了,
只不过你下面 c = a.calcSum(b) 这条算式就得做修改,
你不能再用 c 这个 object 去接传回值。
> }
> };
> int main() {
> Cal a(100), b(200), c(0); //定义 a.x=100, b.x=200
> c = a.calcSum(b); //计算 c.x = a.x + b.x
> // cout<<"c :"<<c<<endl; //如何显示出 (*this).x ??
上面说过,
this 只有在 non-static method 中才能被使用,
而唯有透过 object,
才有办法执行 non-static method,
既然你的 main() 并不在 class 里面,
它显然也并未透过任何 object 来被执行,
那麽在 main() 的程式码中又何来 this 之有?
还是说,
你的意思只是想把 c.x 的内容值印出来?
这样的话那标题就跟内容无关了,
因为你标题问的是跟 this 有关的东西,
所以我也针对跟 this 有关的部分回答你。
> system("PAUSE");
> return 0; //程式正常结束
> }
> 第二个程式
> #include <iostream>
> #include <cstdlib>
> using namespace std;
> class Cal
> {
> int x;
> public:
> Cal(int n) { x = n; } //建立者函数
> Cal *calcSum(Cal obj) { //加法运算
> x += obj.x; //x = x + obj.x
> return this; //传回this->x
这个叫做传回 this。
> }
> };
> int main() {
> Cal a(100), b(200), c(0); //定义 a.x=100, b.x=200
> c = a.calcSum(b); //计算 c.x = a.x + b.x
> // cout<<"c :"<<c<<endl; //如何显示出 this->x ??
在 main 里面没有 this 这种东西。
> system("PAUSE");
> return 0; //程式正常结束
> }
回到你一开头的主题,
你说你写这些程式的目的是为了了解 this 和 *this 的差别,
可是不知道该怎麽写,
这其实非常简单:
#include <iostream>
using namespace std;
class Test {
public:
Test() : val(0) { }
Test(int v) : val(v) { }
// 用来帮助了解 this
void doTest1(Test *ptr)
{
if(ptr == this) cout << "ptr == this" << endl;
else cout << "ptr != this" << endl;
// 附注:在 C++ 这时最好用 reinterpret_cast<void *>() 来转
cout << "ptr: " << (void *)ptr << endl;
cout << "this: " << (void *)this << endl;
}
// 用来帮助了解 *this
bool doTest2()
{
cout << (*this).val << endl;
}
// 暂时不用 private 方便理解
// private:
int val;
};
int main()
{
Test a(1), b(2), c(3);
// 会发现执行 a.doTest1() 时 Test::doTest1() 中的 this 恒等於 &a,
// 将 a.doTest1() 全部换成 b.doTest1() 或 c.doTest1() 再观察看看
a.doTest1(&a);
a.doTest1(&b);
a.doTest1(&c);
// 可发现执行 a.doTest2() 时 Test::doTest2() 中的 *this 就是 a,
// 而执行 b.doTest2() 时 Test::doTest2() 中的 *this 就是 b,
// 以此类推
a.doTest2();
cout << a.val << endl;
b.doTest2();
cout << b.val << endl;
c.doTest2();
cout << c.val << endl;
return 0;
}
--
Name: Tseng, Ling-hua E-mail Address:
[email protected]
School: National Tsing Hua University Department: Computer Science
Interesting: C++, Compiler, PL/PD, OS, VM, Large-scale software design
Researching: Undecided
Homepage:
https://it.muds.net/~uranus
--
╔═══╗ ┼────────────────────────╮
║狂狷 ║ │
* Origin:[ 狂 狷 年 少 ] whshs.cs.nccu.edu.tw ╰─╮
║ 年少║ ┼╮
< IP:140.119.164.252 > ╰─╮
╚╦═╦╝ ╰
* From:61-230-234-91.dynamic.hinet.net
─╨─╨─ KGBBS ─ ◎ 遨翔"BBS"的狂狷不驯;属於年少的轻狂色彩 ◎
[修改]tinlans:61-230-234-91.dynamic.hinet.net 06/11/12 2:19:21