2

I am a bit unclear about the concept of this pointer. I know the this pointer in c++ is hidden pointer and it is used to refer the current invoking object. But I want to know if there is a datatype for this pointer, For eg. int *p; states that p is a pointer to an integer. Similarly this pointer points to what? (I mean where is this 'this' pointer declared or written, or where does it exist and how is it written there.)

Second question is in the context of C# The question is -- what is 'this' in the context of C# if it is not a pointer in C# (I found out it is not a pointer in C# when i tried using the -> arrow operator with 'this' keyword). And again, what is the datatype of 'this' in C# (I know the answer to the above question relating to c++ would answer it, but I wanted to know is it different in C# )

P.S : I am an amateur programmer, and in learning stage, so apologies for using terms like "how is it written there" instead of the proper technical terminologies in asking the question.

Tom
  • 125
  • 1
  • 11
  • C# and C++ are different languages. Why do you expect them to be the same? – default Dec 18 '14 at 13:15
  • I am not expecting them to be same. I just want to know how are they implemented in both the languages. Sorry if the question framing made it look like I consider them to be same. – Tom Dec 18 '14 at 13:17
  • The easiest way to think about the `this` pointer in C++ is as an extra hidden parameter. Imagine you have an object `x` of type `X` with a member function `foo` that takes one `int` parameter. When you do `x.foo(5)`, it is effectively the same as though `foo` had two parameters of type `X*` and `int` and you had done `foo(&x, 5)` instead. – Joseph Mansfield Dec 18 '14 at 13:17
  • @JosephMansfield: The tricky part of that is that the `const` qualification on `this` doesn't actually come from the parameter `x` but from the declaration of `X::foo`. You can call const methods on non-const objects. – MSalters Dec 18 '14 at 13:38

5 Answers5

8

I want to know if there is a datatype for this pointer

The type of the hidden pointer this is always the type of the class where the pointer is used. For example, below

struct Foo {
    void bar1() {
        cout << this << endl;
    }
    void bar2() const {
        cout << this << endl;
    }
};

the type of this inside bar1() is Foo*; inside bar2() const it is const Foo*.

Similarly this pointer points to what?

It is a pointer to the current instance. For example, below

struct Foo {
    void bar() {
        cout << this << endl;
    }
} foo;

the pointers this inside bar() and the expression &foo point to the same object.

what is this in the context of C# if it is not a pointer?

C# has no pointers*, but its concept of object references is reasonably similar. In C#, this represents a reference to the current object. The type of this is the type of the class inside which it is referenced.

Note that the runtime type of the object pointed to by this may be different - for example, it could be a subclass. However, the static type of the pointer this matches the type of the class inside which the pointer this is referenced.

* unless you venture into unsafe context, anyway.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

In C++ the this pointer is an implicit parameter passed to all nonstatic member functions (passed to them as a hidden argument). Have a look here for more explanations: http://www.tutorialspoint.com/cplusplus/cpp_this_pointer.htm and here: http://www.geeksforgeeks.org/this-pointer-in-c/, http://msdn.microsoft.com/en-us/library/y0dddwwd.aspx.

In C# the "this" is a reserved keyword which is a reference to the object for which the method is running. As you know in C# you don't use pointers, but you use references to objects.

msporek
  • 1,187
  • 8
  • 21
0

Here's IMHO a useful reference, that describes the problem pretty fine: MSDN

Nickon
  • 9,652
  • 12
  • 64
  • 119
0

Yes, this pointer in C++ has a type.

class A
{
public:
  void func() {}
}

Inside func type of this is:-

A * const          //for non-const functions.
A const * const    //for const functions.
ravi
  • 10,994
  • 1
  • 18
  • 36
0

this is most certainly a pointer in C++, in class Foo it will be of the type Foo *, unless you're in a volatile or const method - in that case those 2 modifiers will propagate to the this type.

Similarly in C# this is just a magical reference variable of the current object's type.

Niels Keurentjes
  • 41,402
  • 9
  • 98
  • 136