[問題] compiler error請教

看板C_and_CPP作者 (我愛阿蓉)時間16年前 (2010/02/04 01:16), 編輯推噓1(103)
留言4則, 3人參與, 最新討論串1/1
以下是用vc8 寫的小小測試 主要是故意operator=不寫const去run compile error 但是我完全不懂 push_back這行 根本也沒觸發operator = 並且他compile error會說 error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const A' 這我知道它是說我沒寫const 但是 明明就沒跑到他阿 且點兩下這error跳到一個怪異的地方 xutility // TEMPLATE FUNCTION fill template<class _FwdIt, class _Ty> inline void __CLRCALL_OR_CDECL _Fill(_FwdIt _First, _FwdIt _Last, const _Ty& _Val) { // copy _Val through [_First, _Last) _DEBUG_RANGE(_First, _Last); for (; _First != _Last; ++_First) *_First = _Val;<=========這行... } 想問說明明沒有呼叫operator=他什麼機制會讓compiler判斷到這件事呢 如果push_back那行遮掉就啥事都沒發生了 謝謝 以下是code: #include <iostream> #include <vector> using namespace std; class A { public: A():y(new int(5)){} A(const A& rhs){cout<<"copy consA"<<endl;} A& operator = ( A& rhs){cout<<"opt="<<endl; return *this;} }; int main() { A a; vector <A> p; p.push_back(a); return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 123.192.105.166

02/04 01:35, , 1F
02/04 01:35, 1F

02/04 10:46, , 2F
你的 code 沒用到,但 vector 內有用到
02/04 10:46, 2F

02/04 12:40, , 3F
可是如果我加了const讓她可以用到 應該印個cout一下
02/04 12:40, 3F

02/04 12:40, , 4F
可是現在他根本沒印 感覺他沒用到阿
02/04 12:40, 4F
文章代碼(AID): #1BQQzHfG (C_and_CPP)