what does 'this' mean if it static

O

opistobranchia

I was on a job interview and the interviewer asked what "this" meant.
I replied it represented this class like cout << a or cout << this.a
then he asked what this meant if it was static?

I replied I am not really sure but explained I knew what static was.
Now I am still wondering....What do you think the answer was?

Thanks
 
G

GB

opistobranchia said:
I was on a job interview and the interviewer asked what "this" meant.
I replied it represented this class

It is present only in the context of member functions, and points to the
object on which the member function was called. For example, if you
invoke myObj->func(), then within func, "this" will be equal to myObj
(unless some trickery involving overloading the -> operator can change
this; I'm not sure).
then he asked what this meant if it was static?

If what was static? The "this" pointer? The "this" pointer cannot be static.
I replied I am not really sure but explained I knew what static was.
Now I am still wondering....What do you think the answer was?

If a member function is static, there is no "this" pointer.

Gregg
 
V

Victor Bazarov

GB said:
It is present only in the context of member functions,

Should be
"in the context of non-static member functions,"
and points to
the object on which the member function was called. For example, if
you invoke myObj->func(), then within func, "this" will be equal to myObj
(unless some trickery involving overloading the -> operator can change
this; I'm not sure).


If what was static?

The member function, silly.
The "this" pointer? The "this" pointer cannot be
static.

If a member function is static, there is no "this" pointer.

Finally!
 
G

GB

Victor said:
The member function, silly.

What member function? He did not mention anything about a member
function. I did not want to assume that the OP knows that "this" has to
do with member functions.

Gregg
 
V

Victor Bazarov

GB said:
What member function? He did not mention anything about a member
function. I did not want to assume that the OP knows that "this" has
to do with member functions.

That means you assumed he knew absolutely nothing about 'this' or about
member functions... That's just as bad as assuming the OP did know what
'this' has to do with member functions.
 
O

opistobranchia

That means you assumed he knew absolutely nothing about 'this' or about
member functions... That's just as bad as assuming the OP did know what
'this' has to do with member functions.
What is OP?
So this can not represent static stuff? Cool I did not know that but
will next time. It seemed like a trick question.
 
K

kieran

If what was static?
Did he not mean if 'a' was static, e.g.
this->a
He never mentioned member functions.
Sorry to confuse things slightly further,
Kieran
 
I

Ian

opistobranchia said:
What is OP?
So this can not represent static stuff? Cool I did not know that but
will next time. It seemed like a trick question.
Original Poster

To expand, non-static member functions are called on an instance of an
object and have a hidden first parameter, this, which is the instance.
Static member functions do not, they are called on the object rather
than an instance of it.

Take the following example:

struct X
{
void A();
static void B();
};

The C equivalent of the two member functions would be

void A( X* this );
void B();

Ian
 
G

GB

opistobranchia said:
What is OP?
So this can not represent static stuff? Cool I did not know that but
will next time. It seemed like a trick question.

OP is "original poster", (you!). The subject of the post, "What does
'this' mean if it (is) static" could be construed to suggest that the
question is about "this" being static. I did not want to leave any
doubt, so I asked.

It is easy to see why a static member does not have a "this" pointer
once you consider that static members do not operate on an object of
their class (so what would a "this" pointer point to?).

Gregg
 
G

Gianni Mariani

Did he not mean if 'a' was static, e.g.
this->a
He never mentioned member functions.
Sorry to confuse things slightly further,
Kieran

That's quite possibly what the interviewer meant.

i.e.

struct A
{
static int x; //x.1

int func()
{
int x; //x.2
return this->x; // means x.1
}
};

.... perfectly valid code. In this case the "this" means the scope of
the class A - namely it should find x.1 and not x.2.
 
R

RobH

Gianni said:
That's quite possibly what the interviewer meant.

i.e.

struct A
{
static int x; //x.1

int func()
{
int x; //x.2
return this->x; // means x.1
}
};

... perfectly valid code. In this case the "this" means the scope of
the class A - namely it should find x.1 and not x.2.
The this refers to the struct A object struct is the same as class
except all the members are public by default yada yada ....
 
E

Edwin Castro

struct A
You can't access a static member using '->'. You need to use '::' as in:

struct A
{
static int x; //x.1

int function()
{
int x; //x.2
return A::x; //means x.1
}
};
 
G

Greg Comeau

You can't access a static member using '->'. You need to use '::' as in:

struct A
{
static int x; //x.1

int function()
{
int x; //x.2
return A::x; //means x.1
}
};

Perhaps unfortunately, but actually you can.
Normally your alternative is preferred though.
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top