Getting reference of class member with "this" access

Hi,

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?
Last edited on
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.

http://doc.qt.io/qt-4.8/qlist.html#at

If you will not use the pointer to modify the integer you can change the type of myIntPointer to int const* and then your two first attempts should work.

If you don't want const you will have to use the subscript operator.

http://doc.qt.io/qt-4.8/qlist.html#operator-5b-5d
Thank's for your reply.

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??)?
To the left of it.

You know, you don't need to use this-> at all in your code?
Hi,
To the left of it.
int *myIntPointer = &this->listOfIntegers[4]; gives me the following error: "cannot convert 'QList<int>*' to 'int'"

You know, you don't need to use this-> at all in your code?

Yes I know, but I have some locale variables in the function that have the same or similar name so I have a better overview...
listOfIntegers is a pointer so you have derefence using *.

 
int *myIntPointer = &(*this->listOfIntegers)[4];


It is possible to use -> but it doesn't look very nice with operators because then you have call the operator using function syntax.

 
int *myIntPointer = &this->listOfIntegers->operator[](4);
Last edited on
Ah yeah, now it works! Thank you.

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...
http://en.cppreference.com/w/cpp/language/operator_precedence

*this->listOfIntegers is parsed as *(this->listOfIntegers)
Ahh ok great. Thank you both again!

Cheers,
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...
Last edited on
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.
Oh I did not know that. So there is really no sense in using a list as pointer... Ok thank's, that's good to know :-)
Yes I know, but I have some locale variables in the function that have the same or similar name


that is why you should use the convention of nomenclature for type declaration .
Topic archived. No new replies allowed.