I've got a class "TestClass" with a list (Qt list as pointer) that holds non-pointer integers and I'd like to get the adress of these non-pointer integers with "this->listOfIntegers->at(x)":
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class TestClass
{
public:
QList<int> *listOfIntegers;
TestClass();
};
TestClass::TestClass()
{
int *myIntPointer = &this->listOfIntegers->at(4); // <--- doesn't work!
int *myIntPointer = &(this->listOfIntegers->at(4)); // <--- doesn't work!
int *myIntPointer = this->&listOfIntegers->at(4); // <--- doesn't work!
int *myIntPointer = this->listOfIntegers->&at(4)); // <--- doesn't work!
}
How is it possible to get the adress of a non-pointer array member via "this->array->..." access?
The at function returns a reference to const, so when you use the & operator it gives you a pointer to const which you can't assign to a pointer to non-const.
If you will not use the pointer to modify the integer you can change the type of myIntPointer to intconst* and then your two first attempts should work.
If you don't want const you will have to use the subscript operator.
Unfortunatelly, the following still doesn't work: int *myIntPointer = this->listOfIntegers[4];Do I still need to use the "&"? If yes, where should I write it to (behind "this", in front of it??)?
Can you explain to me why I have to write the dereferencing sign '*' left to 'this' instead of left to 'listOfIntegers' ? I mean, I don't want to use the dereference of 'this' but the dereference of the list...
Why is listOfIntegers a pointer? Why not just make it a member, especially since you seem to expect the list to always be present (even if it's empty).
That is because it is not a list of integers but a list of complex classes (I just did not want to complicate the code). I initialize this list at program start and it has a static number of members (31 semi-big classes) that need to be present till program end. I think such big stuff should be stored dynamically, shouldn't it?
That is also the reason why I'd like to have its members as pointers while updating, so I can directly update the list without using too much memory...
List already stores all elements dynamically, so this is not a problem. Only reason why you might want pointer to list, if class does not manage it, does not create it in constructor, and does not delete it in destructor. In this case pointer is threated as simple observing pointer.