this pointer

S

stub

Some questions about "this" pointer:

1. To make an assignment function,

Stribg &String::eek:perator=(const String &other)
{
...
return *this;
}

I have hard time to understand why "*this" matches a "String &" here. Could
someone shed some light on this?

2. In a static member function aren't there "this" pointer accessible inside
it?

3. In a const member function is the "this" pointer of const type while
non-const type in a non-const member?

Thanks in advance!
 
A

Ali R.

Some questions about "this" pointer:
1. To make an assignment function,

Stribg &String::eek:perator=(const String &other)
{
...
return *this;
}

The return type is a String & (String reference) and the assignment
operator is passing back a reference to itself one the assignment is done.
I have hard time to understand why "*this" matches a "String &" here. Could
someone shed some light on this?

2. In a static member function aren't there "this" pointer accessible inside
it?

No there is no this pointer in a static member function.
3. In a const member function is the "this" pointer of const type while
non-const type in a non-const member?
Yes the this pointer is a const pointer in a const member function.
 
J

Jacek Dziedzic

stub said:
Some questions about "this" pointer:

1. To make an assignment function,

Stribg &String::eek:perator=(const String &other)
{
...
return *this;
}

I have hard time to understand why "*this" matches a
"String &" here. Could someone shed some light on this?

You return *this in an assignment operator so that
it's possible to do things like:

a=b=c; // a,b,c are String's and
// b returns (*this) so that a can be assigned
2. In a static member function aren't there "this" pointer
accessible inside it?

Static methods have no "this" pointer, because they
are not associated with a particular instance of an object.

HTH,
- J.
 

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
474,145
Messages
2,570,826
Members
47,372
Latest member
LucretiaFo

Latest Threads

Top