OK I have somplifed my code to this which is surprising to me:
#include <QtCore/QCoreApplication>
#include <QHash>
#include <iostream>
QHash<const char*,const char*> *config;
int main(int argc, char *argv[])
{
using std::cout;
char *key1, *val1;
config = new QHash<const char *,const char *>;
key1 = new char[5];
key1[0] = 'p';
key1[1] = 'o';
key1[2] = 'r';
key1[3] = 't';
key1[4] = '\0';
val1 = new char[2];
val1[0] = '5';
val1[1] = '\0';
config->insert(key1,val1);
if(!(config->contains("port")))
cout<<"port is not a key";
return a.exec();
}
#include <QtCore/QCoreApplication>
#include <QHash>
#include <iostream>
QHash<const char*,const char*> *config;
int main(int argc, char *argv[])
{
using std::cout;
QCoreApplication a(argc, argv);
char *key1, *val1;
config = new QHash<const char *,const char *>;
key1 = new char[5];
key1[0] = 'p';
key1[1] = 'o';
key1[2] = 'r';
key1[3] = 't';
key1[4] = '\0';
val1 = new char[2];
val1[0] = '5';
val1[1] = '\0';
config->insert(key1,val1);
if(!(config->contains("port")))
cout<<"port is not a key";
return a.exec();
}
To copy to clipboard, switch view to plain text mode
When run it prints
port is not a key.
Now of course there is something a bit fishy in my code: i am mixing char * and const char *. I would never have expected expected it could lead to this!!
I should point out I was a C programmer a long time ago and I am not used to the const
word.
Bookmarks