Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C++, why isn't the this keyword a reference? [duplicate]

Tags:

c++

Possible Duplicate:
Why ‘this’ is a pointer and not a reference?

Is there a good reason that this is a pointer instead of a reference in C++?

like image 415
zneak Avatar asked Aug 24 '10 20:08

zneak


People also ask

Why is this a pointer and not a reference?

Differences between pointers and references in C++ A pointer in C++ is a variable that holds the memory address of another variable. A reference is an alias for an already existing variable. Once a reference is initialized to a variable, it cannot be changed to refer to another variable.

Do I need to use this in CPP?

The answer is that C++ utilizes a hidden pointer named “this”! Let's take a look at “this” in more detail. The following is a simple class that holds an integer and provides a constructor and access functions. Note that no destructor is needed because C++ can clean up integer member variables for us.

What is the “this” keyword in C #?

The "this" keyword in C# is a reference to a class or a struct itself. In this article, you'll learn what the C# "this" keyword is, and how and when to use the "this" in C#. The C# “this” keyword represents the “this” pointer of a class or a stuct. The this pointer represents the current instance of a class or stuct.

What is the use of the this keyword in a class?

The this keyword is used to reference the current instance of a class, or an object itself, if you will. It is also used to differentiate between method parameters and class fields if they both have the same name. If you want to go extremes, you can also use the keyword to call another constructor from a constructor in the same class.

What is the use of this keyword in C sharp?

Csharp Programming Server Side Programming The “this” keyword in C# is used to refer to the current instance of the class. It is also used to differentiate between the method parameters and class fields if they both have the same name. Another usage of “this” keyword is to call another constructor from a constructor in the same class.

What are the different types of references in C #?

Here are the following Types of References in C# 1. Class Class is one of the C# reference types and they can be declared using the keyword class. The syntax to declare a class in C# is shown below:


2 Answers

The this concept was introduced before the reference concept was. At the time, this had to be a pointer.Source

like image 83
GManNickG Avatar answered Oct 09 '22 08:10

GManNickG


From Stroustrup's FAQ

http://www2.research.att.com/~bs/bs_faq2.html#this

like image 24
Steve Townsend Avatar answered Oct 09 '22 06:10

Steve Townsend