MiJyn
MiJyn

Reputation: 5867

Pointer to this in C++

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?

Upvotes: 3

Views: 9545

Answers (5)

Peter Al
Peter Al

Reputation: 378

As other replies/comments pointed out, this is a pointer by itself.

Even if you could take a pointer to this: this is a local variable, local to the method you are running, and it is a pointer to the object you are working with.

Returning pointer to this would have the same effect as returning pointer to any other local variable: it will return garbage, because you've just left the scope where this was defined.

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?

Return this, store it in your array, and access like this: myArray[idx]->foo("bar")

Again, this is a pointer to your object.

EDIT: I stand corrected. this is a rvalue expression implemented as hidden method parameter - which has a lifespan of a local variable. :)

Upvotes: 0

lvella
lvella

Reputation: 13443

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 this pointer, 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:

foo** evil_ptr_to_ptr = new foo*(this);
ARRAY.push_back(evil_ptr_to_ptr);

and some time in the future you will have to delete it:

// assuming you got all the elements in the array in the same way:
for(int i = 0; i < ARRAY.size(); ++i) {
  delete ARRAY[i];
}

Upvotes: 12

tomasz
tomasz

Reputation: 13062

You have plenty of correct answers by now, but try to think about it differentely:

Assume you have an object of your class foo allocated at some address in your memory. This address is effectively represented by this in the scope of your class. Now, you say you need yet another address to some variable that keeps the address of original object. This variable would have to be declared and have the lifetime of your object; obviously such a variable does not exist by default as it wouldn't make too much sense to create one. If you want to keep foo** in your container, you most probably have to keep another container of foo*, so the former can point to the latter. Well, again, most probably there's no point in doing so and I guess you should solve you problem differently? (You might like to present the problem on SO and get some help.)

Upvotes: 0

Gigi
Gigi

Reputation: 4962

The this pointer is passed as a hidden argument to all nonstatic member function calls and is available as a local variable within the body of all nonstatic functions.

So, taking the address of the this pointer is effectively meaningless.

Consider this non-static member function:

void my_struct::my_func(int a);

When you call it on a my_struct the compiler do this under the hood:

void my_struct__my_func(my_struct* this, int a);

Given the fact that the this pointer is passed by-value, taking its address will give you the address of something that will not exist anymore after the function return.

Upvotes: 2

AnT stands with Russia
AnT stands with Russia

Reputation: 320481

In C++ this is not an lvalue. For this reason it is not possible to create a pointer to this. For the very same reason it is also not possible to bind a reference directly to this.

Upvotes: 4

Related Questions