2015-02-06 82 views
0
#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; 
} 

问题>我需要使用的ClassOne地址作为std::map的关键。哪一个是最好的选择?都使用const指针或指针作为'的std ::关键map`

谢谢

+1

你*有*使用指针作为关键?你知道使用指针作为关键的陷阱吗? – 2015-02-06 20:07:27

回答

0

这取决于你真正想做的事 - 然而,你很少想改变密钥,所以ClassOne * const如果你不打算改变指针;如果你不想改变指针指向的对象,const ClassOne * const