Dynamic container
-
Hello,
I'm looking for dynamic container. I used QLinkedList, QList, QVector, QMap, QHash but none of them are dynamically.
What actually I'm looking for or what I mean?
@int b = 5;Container cont;
cont[0] = b; // syntax like QMapb = 10;
qDebug() << cont[0]; // Output 10@
Define parameter with value "a"
Add to container
Change parameter to value "b"
Debugging container -> output is "b"
-
looking at your example why don't you just use an array of items? Doesn't really seem like you need it to be "dynamic":http://www.cplusplus.com/doc/tutorial/dynamic/.
If you have a Qlist<int> you can change the int at any valid node you want.
@void foo()
{
QList<int> list_of_ints;
list_of_ints.append(12);
qDebug() << list_of_ints[0];
lis_of_ints[0] = 30;
qDebug() << list_of_ints[0];
list_of_ints.replace(0, 42);
qDebug() << list_of_ints[0];}@
-
Hi, you need to use pointers(or references) instead values.
Or you can take a look at the source code of a dynamic language interpreter, such as Python, to see in which way c pointers are used.
[quote author="glararan" date="1375221456"]
What actually I'm looking for or what I mean?
@int *b;
*b = 5;Container cont;
cont[0] = b; // syntax like QMap*b = 10;
qDebug() << *cont[0]; // Output 10@
Define parameter with value "a"
Add to container
Change parameter to value "b"
Debugging container -> output is "b"
[/quote]
-
As 1+1=2 states, you can store pointers or references instead. Alternatively, you can store explicitly shared values in the container. There really are not much more than a nice value-based wrapper around a shared data pointer.
Note that all Qt containers take copies of the values you put in, so you need to put a type inside your container for which the copy still represents the same underlying data: a reference, a pointer or a construct like an explicitly shared value.