I want to write a line of code that returns a pointer to this, because of an array (std::vector<foo**>). I thought of using a reference (&this), but it didn’t work.
If any clarification is needed, let me know.
EDIT: To clarify what I’m doing, I’m trying to access the object directly using the array. Like so: (*ARRAY.at(i))->foo("bar");. Some people say it is impossible to make a pointer. If so, how would I access the object directly using the array?
You can’t have a pointer to
this, because it is not a variable, it is a reserved keyword that translates to a pointer to the current object.In compilers implementation there may be a local variable of the function backing the
thispointer, but it is an implementation detail and its address is inaccessible to the programmer.What you are trying to do is very evil, but if you really really want to do it, you will have to create the variable yourself:
and some time in the future you will have to delete it: