where is the "this" pointer located?

T

thomas

It's not a member of a class, so where is it located?

It's said that "this" doesn't exist until we call a member function of
an object and it disappear after the function call. My understanding
is as follows. Correct me if I'm wrong.

~~~~~~~~~~~~~~

Every non-static member function call has the first parameter of
"this".
When calling it somewhere, like

----------code----------
struct A{
public:
void f(){}
};
A a;
a.f();
----------code---------

The "a.f()" is filled as "a.f(&a)". So the "this" pointer (&a here) is
automatically inserted by the compiler.
 
R

Rolf Magnus

thomas said:
It's not a member of a class, so where is it located?

It's said that "this" doesn't exist until we call a member function of
an object and it disappear after the function call. My understanding
is as follows. Correct me if I'm wrong.

~~~~~~~~~~~~~~

Every non-static member function call has the first parameter of
"this".
When calling it somewhere, like

----------code----------
struct A{
public:
void f(){}
};
A a;
a.f();
----------code---------

The "a.f()" is filled as "a.f(&a)". So the "this" pointer (&a here) is
automatically inserted by the compiler.

The C++ standard doesn't speicfy this, but the compilers that I know do it
like you say. I couldn't think of another useful approach.
 
A

Alf P. Steinbach

* thomas:
It's not a member of a class, so where is it located?

It's said that "this" doesn't exist until we call a member function of
an object and it disappear after the function call. My understanding
is as follows. Correct me if I'm wrong.

~~~~~~~~~~~~~~

Every non-static member function call has the first parameter of
"this".
When calling it somewhere, like

----------code----------
struct A{
public:
void f(){}
};
A a;
a.f();
----------code---------

The "a.f()" is filled as "a.f(&a)". So the "this" pointer (&a here) is
automatically inserted by the compiler.

That's good enough.

Uh, wait.

When you translate the call you have to think in terms of a freestanding
function, that the compiler translates A::f to something like

void A__f( A* self ) {}

Then the call

a.f()

is (just as hypothetically) translated to

A__f( &a )


Cheers & hth.,

- Alf
 
B

Bo Persson

thomas said:
It's not a member of a class, so where is it located?

It's said that "this" doesn't exist until we call a member function
of an object and it disappear after the function call.

Correct. The 'this' pointer exists inside non-static member functions.
Exactly how that works is up to the compiler (magic).
My
understanding is as follows. Correct me if I'm wrong.

~~~~~~~~~~~~~~

Every non-static member function call has the first parameter of
"this".
When calling it somewhere, like

----------code----------
struct A{
public:
void f(){}
};
A a;
a.f();
----------code---------

The "a.f()" is filled as "a.f(&a)". So the "this" pointer (&a here)
is automatically inserted by the compiler.

That's a common way of getting a 'this' pointer to the function. It s
not required, so some compilers might do it some other way, like
always storing it in a dedicated address register.


Bo Persson
 
B

BGB / cr88192

Bo Persson said:
Correct. The 'this' pointer exists inside non-static member functions.
Exactly how that works is up to the compiler (magic).


That's a common way of getting a 'this' pointer to the function. It s not
required, so some compilers might do it some other way, like always
storing it in a dedicated address register.

or maybe thread local storage...

or, if the compiler were unreasonably crappy and didn't care about threads,
in a global variable...
 
J

James Kanze

It's not a member of a class, so where is it located?
It's said that "this" doesn't exist until we call a member
function of an object and it disappear after the function
call. My understanding is as follows. Correct me if I'm wrong.

Every non-static member function call has the first parameter
of "this". When calling it somewhere, like
----------code----------
struct A{
public:
void f(){}};
A a;
a.f();
----------code---------
The "a.f()" is filled as "a.f(&a)". So the "this" pointer (&a
here) is automatically inserted by the compiler.

Maybe at the implementation level. As far as the language is
concerned, this is a keyword. It's not a variable, or even an
lvalue, so it isn't "located" anywhere. (In most cases, I would
expect to find it in a register somewhere, regardless of how the
compiler passes its other arguments. But that, to, is an
implementation detail.)
 
D

dragan

thomas said:
It's not a member of a class, so where is it located?

It's said that "this" doesn't exist until we call a member function of
an object and it disappear after the function call. My understanding
is as follows. Correct me if I'm wrong.

~~~~~~~~~~~~~~

Every non-static member function call has the first parameter of
"this".
When calling it somewhere, like

----------code----------
struct A{
public:
void f(){}
};
A a;
a.f();
----------code---------

The "a.f()" is filled as "a.f(&a)". So the "this" pointer (&a here) is
automatically inserted by the compiler.

More like:

void mangled_function_name_corresponding_to_A(A* this)
{ }

Calling a.f():

mangled_function_name_corresponding_to_A(&a);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,816
Messages
2,569,706
Members
45,495
Latest member
cberns21

Latest Threads

Top