Thread: 'this' pointer in classes

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    183

    'this' pointer in classes

    hi.
    can anyone plz tell me where the 'this' pointer points to ?
    I mean :
    if 'b' be a object of a class and 'f' be one of the class functions , when we write b.f() -->

    "this" pointer , points to 'b'( object of a class ) or points to 'f' (function of a class ) ?
    tnx

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    can anyone plz tell me where the 'this' pointer points to ?
    I mean :
    if 'b' be a object of a class and 'f' be one of the class functions , when we write b.f() -->

    "this" pointer , points to 'b'( object of a class ) or points to 'f' (function of a class ) ?
    tnx
    The "this" pointer is only applicable within a method function. In that case "this" refers to the object the method function is being called from. For example,

    Code:
    class C
    {
          int x;
    public:
          void f() { this->x = 40; }
    };
    and this->x refers to the variable x of some particular object of class C. What "this" points to his not determined by dynamic execution but by scope. So you can write

    Code:
    class D 
    {
           C c;
           int y;
    public:
           void f() { c.f(); this->y = 100;  }
    };
    D d;
    d.f();
    and "this" in D::f's context points &d during the execution of d.f(), even though in c.f this points to &d.c

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 6
    Last Post: 05-15-2009, 08:38 AM
  3. Why does C need pointer conversion
    By password636 in forum C Programming
    Replies: 2
    Last Post: 04-10-2009, 07:33 AM
  4. Pointer validity check
    By Carlos in forum Windows Programming
    Replies: 6
    Last Post: 12-11-2003, 03:40 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM