Skip to content

Instantly share code, notes, and snippets.

@JohannMG
Created September 24, 2020 03:36
Show Gist options
  • Save JohannMG/afc9b8a68d1feb2bf422f07ed2f0ee46 to your computer and use it in GitHub Desktop.
Save JohannMG/afc9b8a68d1feb2bf422f07ed2f0ee46 to your computer and use it in GitHub Desktop.
Confirm: Passing QMap reference to helper function edits the same QMap.
#include <QCoreApplication>
#include <QMap>
#include <QMapIterator>
#include <QDebug>
using StringIntMap = QMap<const char*, int>;
void addToMap(StringIntMap &map, const char* key, int value)
{
map[key] = value;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
StringIntMap goodmap;
const char* elsewhere = "elsewhere";
goodmap[elsewhere] = 50;
goodmap["justTyped"] = 100;
addToMap(goodmap, "function1", 200);
addToMap(goodmap, "function2", 400);
QMapIterator<const char*, int> iter(goodmap);
while (iter.hasNext()) {
iter.next();
qDebug() << iter.key() << " = " << iter.value();
}
/* prints:
elsewhere = 50
justTyped = 100
function1 = 200
function2 = 400
*/
return a.exec();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment