Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the 'this' keyword not a reference type in C++ [duplicate]

Tags:

c++

this

keyword

Possible Duplicates:
Why ‘this’ is a pointer and not a reference?
SAFE Pointer to a pointer (well reference to a reference) in C#

The this keyword in C++ gets a pointer to the object I currently am.

My question is why is the type of this a pointer type and not a reference type. Are there any conditions under which the this keyword would be NULL?

My immediate thought would be in a static function, but Visual C++ at least is smart enough to spot this and report static member functions do not have 'this' pointers. Is this in the standard?

like image 265
davetapley Avatar asked Feb 11 '10 18:02

davetapley


People also ask

Why is this not a reference c++?

Why is " this " not a reference? Because "this" was introduced into C++ (really into C with Classes) before references were added. Also, I chose " this " to follow Simula usage, rather than the (later) Smalltalk use of "self".

What is mean by this keyword in c#?

this (C# Reference) The this keyword refers to the current instance of the class and is also used as a modifier of the first parameter of an extension method.

What is reference data type in C++?

There are two kinds of references: lvalue references which refer to a named variable and rvalue references which refer to a temporary object.


1 Answers

See Stroustrup's Why is this not a reference

Because "this" was introduced into C++ (really into C with Classes) before references were added. Also, I chose "this" to follow Simula usage, rather than the (later) Smalltalk use of "self".

like image 164
Dario Avatar answered Oct 07 '22 16:10

Dario