Thread: the "this" pointer

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    17

    the "this" pointer

    ok im not real clear on the "this" self pointer thing... and im not sure if anyone has ever tried to google for this and self pointer... but it doesnt exactly show what im looking for.

    is there any way to change the "this" pointer? like to modify the type of "this" pointer... my professor was saying something about that today... but i didnt quite understand. his example was

    Code:
    class SomeClass 
    {
    
    void foo(){
    // the value of "this" here
    }
    
    // is different 
    
    void foo() const {
    // than it is here
    }
    
    };
    i was thinking query method and mutator... but a classmate was saying that its not. something about since normal "this" is a pointer to self it is an integer pointer. but im not sure if he knows what hes talking about.

    anyone have any info?

  2. #2
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    The difference between 'void foo()' and void 'foo()' const is that you can't modify anything in the 'void foo() const'. So a call like:
    Code:
    this->m=k;
    won't work in 'void foo() const', while it's perfectly legal in 'void foo()'.

    is there any way to change the "this" pointer?
    No, this pointers are not modifiable.

    May be your teacher was talking about static member functions? In that case this pointer is not accessible in those.

    im not sure if anyone has ever tried to google for this and self pointer
    I have, just two seconds ago. Have you tried "this pointer c++"? Got alot of good results...
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    A normal member function has a hidden parameter of
    ClassName * const this
    A const member function has a hidden parameter of
    ClassName const * const this
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by MathFan
    The difference between 'void foo()' and void 'foo()' const is that you can't modify anything in the 'void foo() const'. So a call like:
    Code:
    this->m=k;
    won't work in 'void foo() const', while it's perfectly legal in 'void foo()'.
    Close, but not quite. If m is a mutable member, the assignment is allowed, even for const objects or in const methods

    Quote Originally Posted by MathFan
    No, this pointers are not modifiable.
    That's true according to the standard. However, at one time when the standard was in draft, assigning `this' was allowed. Fortunately, the standards committee realised that the cases where it was done were either very dangerous (eg they allowed behaviour that any self-respecting programmer would avoid) or there were better alternatives. So assignment to this disappeared a long time before the C++ standard was actually accepted as a standard.

    But you will find some old compilers that allow assignment to `this', and some libraries that use the ability.

    Quote Originally Posted by MathFan
    May be your teacher was talking about static member functions? In that case this pointer is not accessible in those.
    "Not accessible" is not really a fair description. In the context of static member functions, there is never a `this' pointer, as static members are not invoked for individual objects.

  5. #5
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    If m is a mutable member, the assignment is allowed, even for const objects or in const methods
    Yes, sorry for not being precise enough...


    "Not accessible" is not really a fair description. In the context of static member functions, there is never a `this' pointer, as static members are not invoked for individual objects.
    Absolutely right here too. But, you can say that, it is not accessible because it isn't present...
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by MathFan
    But, you can say that, it is not accessible because it isn't present...


    Well, yeah, literally. In practice, however, a comment "it is not accessible" usually means "it is there but it is not accessible".

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    17
    let me see if i understand this.
    so the this pointer is always a constant pointer(because it points to itself). but wether its a constant pointer to a constant object is dependant upon that const on the right side? That makes sense (as long as im understanding correctly)

    thanks for the info
    Last edited by faze; 06-23-2005 at 11:31 PM.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    so the this pointer is always a constant pointer(because it points to itself).
    The 'this' pointer doesn't "point to itself". Pointers are variables that point to something(except when they are null). For example:
    Code:
    int num = 10;
    int* ptr = #
    'ptr' is the name of a pointer, and it points to num. Similarly, 'this' is the name of a pointer that points to something. What it points to is the object that called the method. For instance, if you have a class called Apple with a method named show(), and you write this code:
    Code:
    Apple myApple;
    myApple.show()
    then inside the show() method, 'this' is the name of a pointer that points to myApple. The method prototype actually looks like this:

    show(Apple* const this);

    However, the 'this' parameter is invisible, and you don't specifically list it for the methods you define. But, because 'this' is an invisible function parameter, you can use the name 'this' inside your methods.

    For constant methods, the compiler makes the object the 'this' pointer points to const as well--otherwise you could use 'this' to change the object. Declaring a method as const is a guarantee that the method won't change the calling object, so the compiler has to ensure you can't change the object.
    Last edited by 7stud; 06-24-2005 at 03:40 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. How did you master pointers?
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-17-2006, 08:23 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Problem with "this" pointer
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 02-07-2002, 10:03 AM