[英]use const pointer or pointer as the key for `std::map`
#include <map>
class ClassOne
{
//...
};
int main() {
std::map< ClassOne *, int > mapA;
std::map< const ClassOne *, int > mapB;
std::map< ClassOne *const, int > mapC;
std::map< const ClassOne * const, int > mapD;
return 0;
}
Question> I need to use address of ClassOne
as the key for std::map
. 问题>我需要使用
ClassOne
地址作为std::map
的键。 which one is the best choice? 哪个是最佳选择?
Thank you 谢谢
This depends on what you actually want to do -- however, you seldom want to change keys, so ClassOne * const
if you're not intending to change the pointer ; 这取决于您实际想要做的事情-但是,您很少想要更改键,所以如果您不打算更改指针 ,那么请使用
ClassOne * const
; if you don't want to change the object that pointer points to, const ClassOne * const
. 如果您不想更改指针指向的对象,
const ClassOne * const
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.