Share this operator in c++

namtupremium

New member
#Toán tử này trong C ++ #C ++ #Operators #Assignent Toán tử #Ternary Toán tử

## Toán tử này trong C ++ là gì?

Toán tử `này trong C ++ đề cập đến đối tượng hiện tại trong hàm thành viên.Nó có thể được sử dụng để truy cập các thành viên dữ liệu và chức năng thành viên của đối tượng hiện tại.

## Làm thế nào để sử dụng toán tử này trong C ++?

Toán tử `` này có thể được sử dụng theo các cách sau:

* Để truy cập các thành viên dữ liệu của đối tượng hiện tại.
* Để gọi các hàm thành viên của đối tượng hiện tại.
* Để chuyển đối tượng hiện tại như một đối số cho một hàm.

### để truy cập các thành viên dữ liệu của đối tượng hiện tại

Để truy cập các thành viên dữ liệu của đối tượng hiện tại, bạn có thể sử dụng toán tử `` này theo tên của thành viên dữ liệu.Ví dụ: mã sau truy cập vào thành viên dữ liệu `name` của đối tượng hiện tại:

`` `C ++
std :: cout << this-> name << std :: endl;
`` `

### để gọi các chức năng thành viên của đối tượng hiện tại

Để gọi các chức năng thành viên của đối tượng hiện tại, bạn có thể sử dụng toán tử `` này theo sau là tên của hàm.Ví dụ: mã sau gọi hàm thành viên `print ()` của đối tượng hiện tại:

`` `C ++
this-> print ();
`` `

### để chuyển đối tượng hiện tại làm đối số cho một hàm

Để chuyển đối tượng hiện tại làm đối số cho một hàm, bạn có thể sử dụng toán tử `này.Ví dụ: mã sau chuyển đối tượng hiện tại dưới dạng đối số cho hàm `foo ()`:

`` `C ++
foo (cái này);
`` `

## Ví dụ về toán tử này trong C ++

Sau đây là một số ví dụ về toán tử `này trong C ++:

`` `C ++
người lớp {
công cộng:
STD :: Tên chuỗi;

void print () {
std :: cout << this-> name << std :: endl;
}
};

int main () {
Người {"John Doe"};

std :: cout << person.name << std :: endl;

người.print ();

trả lại 0;
}
`` `

## Phần kết luận

Toán tử `` này là một công cụ mạnh mẽ có thể được sử dụng để truy cập các thành viên dữ liệu và chức năng thành viên của đối tượng hiện tại.Nó có thể được sử dụng để đơn giản hóa mã và làm cho nó dễ đọc hơn.

## hashtags

* #C ++
* #Operators
* Toán tử #Assignment
* Nhà điều hành #Ternary
=======================================
#This Operator in C++ #C++ #Operators #Assignment Operator #Ternary Operator

## What is the This Operator in C++?

The `this` operator in C++ refers to the current object in a member function. It can be used to access the data members and member functions of the current object.

## How to use the This Operator in C++?

The `this` operator can be used in the following ways:

* To access the data members of the current object.
* To call the member functions of the current object.
* To pass the current object as an argument to a function.

### To access the data members of the current object

To access the data members of the current object, you can use the `this` operator followed by the name of the data member. For example, the following code accesses the `name` data member of the current object:

```c++
std::cout << this->name << std::endl;
```

### To call the member functions of the current object

To call the member functions of the current object, you can use the `this` operator followed by the name of the function. For example, the following code calls the `print()` member function of the current object:

```c++
this->print();
```

### To pass the current object as an argument to a function

To pass the current object as an argument to a function, you can use the `this` operator. For example, the following code passes the current object as an argument to the `foo()` function:

```c++
foo(this);
```

## Examples of the This Operator in C++

The following are some examples of the `this` operator in C++:

```c++
class Person {
public:
std::string name;

void print() {
std::cout << this->name << std::endl;
}
};

int main() {
Person person{"John Doe"};

std::cout << person.name << std::endl;

person.print();

return 0;
}
```

## Conclusion

The `this` operator is a powerful tool that can be used to access the data members and member functions of the current object. It can be used to simplify code and make it more readable.

## Hashtags

* #C++
* #Operators
* #Assignment Operator
* #Ternary Operator
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top