[問題] overload operator

看板C_and_CPP作者 (/* I'm not worth */)時間15年前 (2010/07/29 21:42), 編輯推噓0(005)
留言5則, 3人參與, 最新討論串1/1
#include <iostream> using namespace std; char * operator +(char *a, char *b){ char *temp1 = a; char *temp2 = a; while(a!='\0') { temp1++; } while(b!='\0') { *temp1 = *b; temp1++; } return a; } int main(){ char *str1 = "aBC "; char *str2 = "Tokyo."; char *str3 = str1 + str2; cout << str3 << endl; system("pause"); return 0; } 請問這樣編譯為啥會沒過阿 可以怎麼修改呢? 謝謝!! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 219.70.231.184

07/29 21:43, , 1F
你不能 overload "pointer + pointer"
07/29 21:43, 1F

07/29 21:45, , 2F
內建型態相關的運算子不能 overload
07/29 21:45, 2F

07/29 21:47, , 3F
指標這樣玩, 你可能不知道他背後的涵義...string 類別
07/29 21:47, 3F

07/29 21:47, , 4F
就有的功能就不要自己寫了
07/29 21:47, 4F

07/29 21:55, , 5F
恩恩 THX~
07/29 21:55, 5F
文章代碼(AID): #1CKOL7DP (C_and_CPP)