다음 코드는 operator[]
방법에 const
로지도를 통과하는 규정을 삭제 있다고? 지도 액세스 권한이있는 함수를 const로 선언 할 수 있습니까?C++지도 액세스를 파기 예선 (CONST)는
MapWrapper.cpp:10: error: passing ‘const std::map<int, int, std::less<int>, std::allocator<std::pair<const int, int> > >’ as ‘this’ argument of ‘_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = int, _Tp = int, _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, int> >]’ discards qualifiers
그냥 nitpick,하지만 MW는 MapWrapper 단순히 선언 할 수 있습니다 mw; – luke
좋은 지적 - 몇 가지 언어로 작성하므로 문법을 표준화하여 머리에 맞도록하는 경향이 있습니다. :) – cdleary
나는 그것을 알 수있다. 하지만이 경우에는 필요하지 않은 추가 객체 생성 및 할당이있는 경우에는주의해야합니다. – luke