Answer the question
In order to leave comments, you need to log in
Error in accessing map through operator []?
I'm sorry, the question is most likely completely lamer, but I'm encountering STL for the first time.
There is a code:
char* packetType = "SLogin";
RegisteredEventFn fnForCall = registeredEvents[packetType];
// fnForCall == <указатель на функцию>
char* packetType = (char*)txePacket->Attribute("type");
// packetType указывает на строку "SLogin", строка заканчивается '\0'
RegisteredEventFn fnForCall = registeredEvents[packetType];
// fnForCall == NULL
Answer the question
In order to leave comments, you need to log in
In the second case it doesn't work because char * are mapped by std::map as pointers. Naturally, two different strings (one constant, one from txePacket), albeit having the same values, are located at different addresses and therefore are not equal as pointers.
The question remains why it works in the first case. As an optimization, the compiler merges all identical string constants into one with the same address in a read-only section. Therefore, the literal string "foo" anywhere in the same translation unit will have the same address.
char *x = "...";
you can not do it this way. Literal strings areconst char *
.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question