Re: [問題] operator
※ 引述《tyc5116 (累人啊....)》之銘言:
: class GMMtors
: {
: public:
: GMotor* operator[](String NameIndex) const;
: private:
: map<String,GMotor*> m_MMotors;
: };
: GMotor* GMMtors::operator[](String NameIndex) const
: {
: if (!this->m_MMotors.count(NameIndex))
: {
: return this->m_MMotors[NameIndex]; //這裡出錯
: }
: }
: 本來是想避掉在無形中自己插入新的元素,結果發現這個問題
: 看了錯誤訊息,但不是很懂這個opeerator+是在哪邊會被用到?
: ◆ From: 111.251.163.42
: ※ 編輯: tyc5116 來自: 111.251.163.42 (06/05 16:36)
: 推 shadow0326:map::operator[]非const,不能在const method裡呼叫 06/05 16:44
: 推 iamstudent:那operator[]要寫兩個版本。一個const、一個可寫入 06/05 19:21
這地方我有個問題,上官網看了一下,map::operator[]非const
而我這邊是const,這不算是多載嗎?
另外,像這樣的程式碼,承上面
GMMotor* Obj = new GMMotor();
...
Obj["aa"]->dosomething(); //如果類似這樣的語法要compile通過的話
可以怎麼改呢?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.25.176.145
→
06/05 20:34, , 1F
06/05 20:34, 1F
→
06/05 21:01, , 2F
06/05 21:01, 2F
→
06/05 21:47, , 3F
06/05 21:47, 3F
→
06/05 22:24, , 4F
06/05 22:24, 4F
→
06/05 22:58, , 5F
06/05 22:58, 5F
→
06/05 23:06, , 6F
06/05 23:06, 6F
→
06/06 01:05, , 7F
06/06 01:05, 7F
討論串 (同標題文章)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章