Programming 板


※ 引述《[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







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草
伺服器连线错误,造成您的不便还请多多包涵!
「赞助商连结」






like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Tech_Job站内搜寻

TOP