User Tools

Site Tools


cpp_this_keyword

CPP this keyword

Return to C++ Reserved words, C++, Reserved Words, CPP Glossary, CPP Topics


Certainly! Let's discuss a common C++ reserved word and compare its usage with its equivalents (or closest concepts) in Python, Java, C#, Kotlin, JavaScript, TypeScript, PHP, Go, Rust, Swift, Transact-SQL, and PL/SQL. The reserved word we'll discuss is `this`. The `this` keyword in C++ is a pointer to the current object instance of a class. Let's see how this concept applies or translates into each of the other mentioned languages.

C++

In C++, `this` is a pointer to the current object instance of the class. It's often used in member functions to refer to the calling object.

```cpp class Example { public:

   int x;
   Example(int x) { this->x = x; } // Use of this to distinguish between the parameter x and the field x
}; ```

[C++ Documentation](https://en.cppreference.com/w/cpp/language/this)

Python

Python uses `self` to refer to the instance of the current class, and it's passed explicitly to each instance method, including the initializer. Unlike C++, `self` is not a keyword but a convention.

```python class Example:

   def __init__(self, x):
       self.x = x
```

[Python Documentation](https://docs.python.org/3/reference/datamodel.html#objects-values-and-types)

Java

In Java, `this` is used in a very similar way to C++. It refers to the current object. It's used to access instance variables and methods.

```java public class Example {

   private int x;
   public Example(int x) {
       this.x = x;
   }
} ```

[Java Documentation](https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html)

C#

C# uses `this` similarly to Java and C++. It refers to the current instance of the class. It can be used to access members of the class and to pass the current instance.

```csharp public class Example {

   private int x;
   public Example(int x) {
       this.x = x;
   }
} ```

[C# Documentation](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/this)

Kotlin

Kotlin uses `this` to refer to the current object, much like Java. In addition to its regular use, Kotlin provides the `this` expression to access outer class instances from inner classes.

```kotlin class Example(val x: Int) {

   fun printX() {
       println(this.x)
   }
} ```

[Kotlin Documentation](https://kotlinlang.org/docs/this-expressions.html)

JavaScript

In JavaScript, `this` refers to the context in which the current code is executed. It can refer to different objects depending on how a function is called.

```javascript function Example(x) {

   this.x = x;
} ```

[JavaScript Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this)

TypeScript

TypeScript, being a superset of JavaScript, uses `this` in the same way. TypeScript adds static typing to JavaScript, but the concept of `this` remains the same.

```typescript class Example {

   constructor(public x: number) {}
} ```

[TypeScript Documentation](https://www.typescriptlang.org/docs/handbook/classes.html#understanding-this)

PHP

In PHP, `$this` is used to refer to the current instance of a class. It's similar to other object-oriented languages.

```php class Example {

   public $x;
   function __construct($x) {
       $this->x = $x;
   }
} ```

[PHP Documentation](https://www.php.net/manual/en/language.oop5.basic.php#language.oop5.basic.this)

Go

Go does not have classes but uses structs and receiver functions to achieve method behavior. The receiver function takes a receiver of type struct, which acts similarly to `this` or `self`.

```go type Example struct {

   x int
}

func (e *Example) SetX(x int) {

   e.x = x
} ```

[Go Documentation](https://golang.org/doc/effective_go#methods)

Rust

Rust uses `self` to refer to the instance of the struct or enum the method is called on. It's similar to Python's `self`.

```rust struct Example {

   x: i32,
}

impl Example {

   fn new(x: i32) -> Self {
       Example { x }
   }
} ```

[Rust Documentation](https://doc.rust-lang.org/book/ch05-03-method-syntax.html)

Swift

Swift uses `self` to refer to the instance of the class, struct, or enum. It's similar to other languages' use of `this` or `self`.

```swift class Example {

   var x: Int
   init(x: Int) {
       self.x = x
   }
} ```

[Swift Documentation](https://docs.swift.org/swift-book/LanguageGuide/Methods.html)

Transact-SQL

Transact-SQL does not have a direct equivalent of `this` as it is primarily a database query language. However, in the context of stored procedures or functions, variables and parameters are directly referenced by their names without a need for `this`.

```sql CREATE PROCEDURE ExampleProcedure @X int AS BEGIN

   SELECT @X
END ```

[Transact-SQL Documentation](https://docs.microsoft.com/en-us/sql/t-sql/language-reference?view=sql-server-ver15)

PL/SQL

PL/SQL, like Transact-SQL, is used for managing data in Oracle databases and does not have an equivalent of `this`. Variables and parameters within PL/SQL blocks, functions, or procedures are referenced by name.

```plsql BEGIN

   DECLARE x NUMBER;
   -- Use x directly
END; ```

[PL/SQL Documentation](https://docs.oracle.com/en/database/oracle/oracle-database/19/lnpls/plsql-language-fundamentals.html)

Each of these languages has its own way of referring to the current instance within methods, functions, or procedures, reflecting their unique paradigms and syntax. While object-oriented languages like C++, Java, C#, and Swift use `this` or `self` within class contexts, languages designed for different paradigms or with a different focus, such as Transact-SQL and PL/SQL, do not have a direct equivalent.


Fair Use Sources

C++: C++ Fundamentals, C++ Inventor - C++ Language Designer: Bjarne Stroustrup in 1985; C++ Keywords, C++ Built-In Data Types, C++ Data Structures (CPP Containers) - C++ Algorithms, C++ Syntax, C++ OOP - C++ Design Patterns, Clean C++ - C++ Style Guide, C++ Best Practices ( C++ Core Guidelines (CG)) - C++ BDD, C++ Standards ( C++ 23, C++ 20, C++ 17, C++ 14, C++ 11, C++ 03, C++ 98), Bjarne Stroustrup's C++ Glossary, CppReference.com, CPlusPlus.com, ISOcpp.org, C++ Compilers (Compiler Explorer, MinGW), C++ IDEs, C++ Development Tools, C++ Linter, C++ Debugging, C++ Modules ( C++20), C++ Packages, C++ Package Manager ( Conan - the C/C++ Package Manager), C++ Standard Library, C++ Libraries, C++ Frameworks, C++ DevOps - C++ SRE, C++ CI/CD ( C++ Build Pipeline), C++ Data Science - C++ DataOps, C++ Machine Learning, C++ Deep Learning, Functional C++, C++ Concurrency, C++ History, C++ Topics, C++ Bibliography, Manning C++ Series, C++ Courses, CppCon, C++ Research, C++ GitHub, Written in C++, C++ Popularity, C++ Awesome , C++ Versions. (navbar_cplusplus – see also navbar_cpp_containers, navbar_cppcon, navbar_cpp_core_guidelines, navbar_cpp23, navbar_cpp20, navbar_cpp17, navbar_cpp14, navbar_cpp11)

Reserved Words: Programming Language Keywords, aka Reserved Identifiers. (navbar_reserved_words - see also navbar_programming)


© 1994 - 2024 Cloud Monk Losang Jinpa or Fair Use. Disclaimers

SYI LU SENG E MU CHYWE YE. NAN. WEI LA YE. WEI LA YE. SA WA HE.


cpp_this_keyword.txt · Last modified: 2024/04/28 03:13 by 127.0.0.1