[問題] compiler error請教
以下是用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
02/04 10:46, 2F
→
02/04 12:40, , 3F
02/04 12:40, 3F
→
02/04 12:40, , 4F
02/04 12:40, 4F