|
From: Per W. <pw...@ia...> - 2003-07-10 00:27:29
|
Did I miss something?
The compare function doesn't look for similar addresses - it performs a
full string compare. How would std::string make a difference?
The only important thing is that the users own their different names, and
doesn't have the name point to a auto-declared text buffer used during the
creation.
/Per W
On Thu, 10 Jul 2003, Ioannis Vranos wrote:
>
>
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf Of Shane Bryldt
> Sent: Thursday, July 10, 2003 2:00 AM
> To: Ioannis Vranos
> Cc: C++
> Subject: Re: [Dev-C++] STL Map question
>
>
> Yes, I appologize for not including the header definition. Here it is.
>
> #ifndef USERLIST_H
> #define USERLIST_H
>
> #include <map.h>
>
>
> #include <cmap>
>
>
>
> class User;
>
> class UserList {
> public:
> struct ltstr
> {
> bool operator()(const char* s1, const char* s2) const
> {
> return strcmp(s1, s2) < 0;
> }
>
>
> Why don't you use std::string?
>
>
>
> };
> typedef std::map<const char *, User *, ltstr> UserMap;
>
> UserList();
> ~UserList();
>
> void AddUser(User *pUser);
> bool DeleteUser(const char *pNick);
> User *FindUser(const char *pNick);
> unsigned long Length();
>
> UserMap m_mapUsers;
> };
>
>
> #endif
>
>
>
> This is really dangerous stuff! At first keep in mind that in the code:
>
>
> UserList whatever;
>
> // ...
>
> whatever.FindUser("Johnson");
>
> whatever.FindUser("Johnson");
>
>
> These two "Johnson" have different addresses! So better use std::string.
>
>
> The specific portion of code you had provided and suspected that it crashes,
> seems clean. The crash must occur somewhere else (soemwhere in those char
> *s).
>
> Use std::string now! :-)
>
>
>
>
> Ioannis Vranos
>
> * Programming pages: http://www.noicys.freeurl.com
> <http://www.noicys.freeurl.com/>
> * Alternative URL 1: http://run.to/noicys
> * Alternative URL 2: http://www.noicys.cjb.net <http://www.noicys.cjb.net/>
>
>
>
|