看板Programming
标 题Re: 请教C++的this
发信站政大狂狷年少 (Fri Nov 3 03:50:11 2006)
转信站ptt!ctu-reader!Spring!newsfeed.media.kyoto-u.ac.jp!diablo.efnet.com!ef
※ 引述《[email protected] (我要出清)》之铭言:
> #include <iostream>
> #include <cstdlib>
> using namespace std;
> class work
> {
> public:
> int Id;
> char name[20];
> work() {
> Id = 0;
> strcpy(name, "ZZZ"); //指定name初值
> }
> void output() {
> cout << this->Id << endl;
> cout << (*this).Id << endl;
> }
> };
> int main(void)
> {
> work work1;
> work1.Id=5;
> //work1.name="quota123";
> work1.output();
> system("pause");
> return 0;
> }
> 请教一下 以上程式的
> this->Id 与(*this).Id 我在网路上看到陈老师的网页 他是这样说明:
> 「this」指标代表目前这个物件的指标;
> 「*this」则代表目前这个物件的内容
> 可是.为何我这里得到的答案都是5呢?
你可能必须先学会所有 operator 的意义才行,
ptr->member 只不过是 (*ptr).member 的另一种写法,
你完全把问题重心看偏了。
> 另外我想问.. strcpy这一行程式是我看到范例原本就有的
> 可是 如果我想要给他资料
> 比如work1.name="quota123";
> 却会出现错误
> 请问..work class内的这个成员 我该如何使用呢?
也是用 strcpy(work1.name, "quota123"); 设值,
C-style string 就该照 C-style string 的规则玩,
如果你还不熟,
那建议你最好先搞懂它,
不过也建议你在 C++ 就多用 C++ 的 string,
往後想做其它事情也比较简单。
--
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-217-167.dynamic.hinet.net
─╨─╨─ KGBBS ─ ◎ 遨翔"BBS"的狂狷不驯;属於年少的轻狂色彩 ◎
1F:推 meltice:在BCB我习惯用AnsiString MFC用CString 210.64.13.208 11/03 12:51