this

A

asdf

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

Ian Collins

asdf said:
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?
 
S

Steve Pope

asdf said:
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
 
I

Ian Collins

Ian said:
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.
 
D

Daniel T.

asdf said:
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.
 
F

Frederick Gotham

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

Daniel T.

Frederick Gotham said:
asdf:


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?
 
G

Guest

Daniel said:
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::print
}
virtual void print() {
std::cout << "Base::print()\n";
}
};
class Derived : public Base {
public:
Derived() : Base() {
std::cout << "Derived(): *this is " << typeid(*this).name() <<
'\n';
print(); // Derived::print
}
virtual void print() {
std::cout << "Derived::print()\n";
}
};
int main() {
Derived d;
Base *b = &d;
b->print(); // Derived::print
}
 
H

hankssong

"this" pointer is the address of every object, so it is unique.
"asdf дµÀ£º
"
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top