简体   繁体   中英

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 . 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 ; if you don't want to change the object that pointer points to, const ClassOne * const .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM