Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > this

Reply
 
 
asdf
Guest
Posts: n/a
 
      11-26-2006
How to understand "this" pointer? It is so tricky, I read several books
but couldn't figure it out.

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      11-26-2006
asdf wrote:
> How to understand "this" pointer? It is so tricky, I read several books
> but couldn't figure it out.
>

Then they can't be very good.

What specific questions do you have?

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Steve Pope
Guest
Posts: n/a
 
      11-26-2006
asdf <(E-Mail Removed)> wrote:

>How to understand "this" pointer? It is so tricky, I read several books
>but couldn't figure it out.


It's not very tricky at all. Here's my (amateur) explanation:

When executing any C++ statement, there are only two possibilities:
either the statement is part of a method for an object, or it
isn't. If it isn't part of a method, you cannot use "this". But if
it is part of a method, then this->x is a way of referring to
a data member "x" of that object.

The type of "this" is a pointer to the type of the object. Therefore
"this" can get passed into functions where the member "x" itself
would not be within scope.

Hope this helps.

Steve
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      11-26-2006
Ian Collins wrote:
> asdf wrote:
>
>>How to understand "this" pointer? It is so tricky, I read several books
>>but couldn't figure it out.
>>

>
> Then they can't be very good.
>
> What specific questions do you have?
>

I forgot to mention the same question was asked on or about the 28th of
June, look back through the archives.

--
Ian Collins.
 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      11-26-2006
"asdf" <(E-Mail Removed)> wrote:

> How to understand "this" pointer? It is so tricky, I read several books
> but couldn't figure it out.


In the language Python, "this" is a little more explicit (although they
call it "self".)

class Foo:
def bar(self):
self.thing = 0

def bar1(self, arg):
self.thing = arg


The above two methods (member-functions) would be called as follows:

myFoo = Foo()

myFoo.bar()

anArg = 3
myFoo.bar( anArg )

Read more at www.python.org...

"this" is the implied "first argument" of every member-function. It is
the object the function is called on, the thing to the left of the dot
operator.

If you have a more specific question, by all means ask.

--
To send me email, put "sheltie" in the subject.
 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      11-26-2006
asdf:

> How to understand "this" pointer? It is so tricky, I read several books
> but couldn't figure it out.



You can use "this" within a member functions (including constructors,
destructors and operator overloads). It's a pointer to the current object.

Obviously, there's no "this" within a static member function, as there's no
current object.

--

Frederick Gotham
 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      11-26-2006
Frederick Gotham <(E-Mail Removed)> wrote:
> asdf:
>
>> How to understand "this" pointer? It is so tricky, I read several
>> books but couldn't figure it out.

>
> You can use "this" within a member functions (including
> constructors, destructors and operator overloads). It's a pointer to
> the current object.


I'd like to ask a bit about use of 'this' in the constructor. Until the
constructor exits, the object doesn't officially exist right? I know
virtual function calls should be avoided, and I find myself wondering,
what exactly does 'this' represent in the constructor?

--
To send me email, put "sheltie" in the subject.
 
Reply With Quote
 
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
Guest
Posts: n/a
 
      11-26-2006
Daniel T. wrote:
> Frederick Gotham <(E-Mail Removed)> wrote:
> > asdf:
> >
> >> How to understand "this" pointer? It is so tricky, I read several
> >> books but couldn't figure it out.

> >
> > You can use "this" within a member functions (including
> > constructors, destructors and operator overloads). It's a pointer to
> > the current object.

>
> I'd like to ask a bit about use of 'this' in the constructor. Until the
> constructor exits, the object doesn't officially exist right?


It does, but its type may not be what you expect.

> I know
> virtual function calls should be avoided, and I find myself wondering,
> what exactly does 'this' represent in the constructor?


In X's constructor, "this" is a pointer to an object of type X. It's a
complete object, and you can use it any way you could any other object,
but it will always be treated as being of type X, not any derived type.

#include <iostream>
#include <typeinfo>

class Base {
public:
Base() {
std::cout << "Base(): *this is " << typeid(*this).name() <<
'\n';
print(); // Base:rint
}
virtual void print() {
std::cout << "Base:rint()\n";
}
};
class Derived : public Base {
public:
Derived() : Base() {
std::cout << "Derived(): *this is " << typeid(*this).name() <<
'\n';
print(); // Derived:rint
}
virtual void print() {
std::cout << "Derived:rint()\n";
}
};
int main() {
Derived d;
Base *b = &d;
b->print(); // Derived:rint
}

 
Reply With Quote
 
hankssong
Guest
Posts: n/a
 
      11-28-2006
"this" pointer is the address of every object, so it is unique.
"asdf д
"
> How to understand "this" pointer? It is so tricky, I read several books
> but couldn't figure it out.


 
Reply With Quote
 
hankssong
Guest
Posts: n/a
 
      11-28-2006
"this" pointer is the address of every object, so it is unique.
"asdf д
"
> How to understand "this" pointer? It is so tricky, I read several books
> but couldn't figure it out.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments