3

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?

14
  • 6
    std::vector<foo**> looks like a very bad idea. Commented Jul 2, 2012 at 18:26
  • this is a pointer, specifically to the instance of the object. To return a pointer to this implies changing the internal instance from outside the class. That would probably be bad. What are you really doing? Commented Jul 2, 2012 at 18:26
  • "I thought of using a reference (&this), but it didn't work." Do you mean *this? Commented Jul 2, 2012 at 18:26
  • 2
    Clarifications are needed. Commented Jul 2, 2012 at 18:32
  • 1
    If you just want to access it directly using an array, why can't you just put this into the array's element? somearrayofpointers[0]=this; ? Commented Jul 2, 2012 at 18:33

5 Answers 5

12

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];
}
Sign up to request clarification or add additional context in comments.

4 Comments

@lkjoel using the address operator (&). It returns the address of the pointer on the stack of the current function.
@Richard, I tried that (&this), but it gives an error message: error: lvalue required as unary ‘&’ operand
@RichardJ.RossIII - no. It is not possible to use the & on this. The this pointer is not an l-value. It is an expression (albeit an special one), like 2+1. It is like trying to compute &(2+1), and in both cases, you will get the same error, that the operant of the unary & operator is not an l-value.
And whoever down-voted Ivella's answer is not aware of his own ignorance. Ivella's answer (like AndreyT's above) is correct (and ergo, +1 for it.)
4

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.

9 Comments

Interesting. Why isn’t it a (const) lvalue though? It certainly looks like one (it has a name and all).
@Konrad Rudolph: Because when it comes to rvalues of scalar types, the concept of const does not apply at all. Rvalues of scalar types cannot be const or non-const. See 3.10/9: "Class rvalues can have cv-qualified types; non-class rvalues always have cv-unqualified types".
overloaded assignment operators regularly return a reference to this ... I guess that is an rvalue?
@Andrey My question was why it’s not an lvalue, though; not rvalue.
@AJG85 Overloaded operators return references to *this which is a different beast entirely.
|
2

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.

Comments

0

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.)

Comments

0

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. :)

2 Comments

this is not a local variable, this is a lvalue expression that evaluates to the pointer of the current object being called. That said, they are usually implemented as function parameter internally by the compiler, what is not visible to the user.
@lvella: this is an rvalue expression that evaluates to the pointer of the current object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.