C++ General: What is the 'this' pointer?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: C++ General: What is the 'this' pointer?

Threaded View

  1. #1
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    C++ General: What is the 'this' pointer?

    Q: What is the 'this' pointer?

    A: It is a misbelief that the 'this' pointer is a hidden member of a class or struct. It is a hidden parameter of non-static member functions. When you declare a function the compiler adds an extra parameter to function's prototype. The type of the parameter depends on how the function is declared. According to C++ standard, 9.3.2.1:
    In the body of a nonstatic member function, the keyword this is a non-lvalue expression whose value is the address of the object for which the function is called. The type of this in a member function of a class X is X*. If the member function is declared const, the type of this is const X*, if the member function is declared volatile, the type of this is volatile X*, and if the member function is declared const volatile, the type of this is const volatile X*.
    For instance
    Code:
    class T
    {
    public:
      void foo(int a);
      int goo() const;
    };
    is actually:
    Code:
    class T
    {
    public:
      void foo(T* this , int a);
      int goo(const T* this) const;
    };
    Static member functions, which donít have class scope, do not have this extra parameter. One consequence is that you cannot use a non-static member function as a thread function even if it has the correct prototype
    Code:
    UINT ThreadFunction(LPVOID param);
    because that in fact the prototype (when non-static) is
    Code:
    UINT ThreadFunction(T* this, LPVOID param);

    Last edited by Andreas Masur; July 23rd, 2005 at 02:09 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center