this

Discussion in 'C++' started by asdf, Nov 26, 2006.

  1. asdf

    asdf Guest

    How to understand "this" pointer? It is so tricky, I read several books
    but couldn't figure it out.
    asdf, Nov 26, 2006
    #1
    1. Advertising

  2. asdf

    Ian Collins Guest

    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.
    Ian Collins, Nov 26, 2006
    #2
    1. Advertising

  3. asdf

    Steve Pope Guest

    asdf <> 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
    Steve Pope, Nov 26, 2006
    #3
  4. asdf

    Ian Collins Guest

    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.
    Ian Collins, Nov 26, 2006
    #4
  5. asdf

    Daniel T. Guest

    "asdf" <> 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.
    Daniel T., Nov 26, 2006
    #5
  6. 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
    Frederick Gotham, Nov 26, 2006
    #6
  7. asdf

    Daniel T. Guest

    Frederick Gotham <> 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.
    Daniel T., Nov 26, 2006
    #7
  8. Daniel T. wrote:
    > Frederick Gotham <> 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::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
    }
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Nov 26, 2006
    #8
  9. asdf

    hankssong Guest

    "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.
    hankssong, Nov 28, 2006
    #9
  10. asdf

    hankssong Guest

    "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.
    hankssong, Nov 28, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page