this


this

In C++, this is a pointer to the object that invokes a member function. In Java and C#, this is a reference to the object that invokes a member function. Each time a member function is invoked, it is automatically passed a pointer (or a reference in Java and C#), called this, to the object that has invoked it. The this pointer is an implicit parameter to all member functions. Therefore, inside a member function, this may be used to refer to the invoking object. The this pointer (or reference) can be used to access any member variable from a member function of the class.
En C++, this es un puntero al objeto que llama a una función miembro. En Java y C#, this es una referencia al objeto que llama a una función miembro. Cada vez que una función miembro es llamada, a ésta se le pasa automáticamente un puntero (o una referencia en Java y en C#), llamada this, al objeto que ha llamado a la función. El puntero this es un parámetro implícito en todas las funciones miembro. Por lo tanto, dentro de una función miembro, this puede usarse para referir al objeto presente. El puntero (o referencia) this puede ser usado para accesar cualquier variable miembro desde una de las funciones miembros de la clase.

Tip
The Book class is defined in the files: Book.h and Book.cpp. Observe how the functions are implemented, note how the this pointer (or reference) may or may not be used to call member functions or have access to the member variables. In some cases, the this pointer makes the code harder to read. Some programmer use only the keyword this when there is a local variable and member variable with the exact same name, this will provide access to the member variable.
La clase Book está definida en los archivos: Book.h y Book cpp. Observe como estas funciones están implementadas, note como el puntero (o referencia) this puede o no puede ser usado para llamar a las funciones miembro o tener acceso a las variables miembro. En algunos casos, el puntero this hace el código más difícil de leer. Algunos programadores usan solamente la palabra clave this cuando hay una variable local y una variable miembro con el mismo nombre, this proporcionará acceso a la variable miembro.

Book.h
#pragma once
class Book
{
public:
     Book(void);
     ~Book(void);
     COLORREF color;
     int numbPages;
     wstring author;
     int year;
     double price;
     void Reset();
     void IncreasePrice(double price);
};

Book.cpp
#include "StdAfx.h"
#include "Book.h"


Book::Book(void)
{
     //Reset();
     this->Reset();
}


Book::~Book(void)
{
}

void Book::Reset()
{
     //color = RGB(0, 0, 0); // Default is black
     //numbPages = 0;
     //year = 1990;
     //price = 0.0;

     this->color = RGB(0, 0, 0); // Default is black
     this->numbPages = 0;
     this->year = 1990;
     this->price = 0.0;     
}

void Book::IncreasePrice(double price)
{
     // this->price is the member variable
     // and price without this is a local variable
     this->price = price;
}

Tip
When writing code in C++, you may start by typing this-> so that the code editor list the member functions and member variables of the current object, optionally you may keep typing some characters to narrow down the search, when ready press the Enter key to save some typing. In C# and Java, you may start by typing this.

Outside the class file(s), you may use the dot operator to list the members of the class.
Cuando escriba código en C++, usted puede comenzar tecleando this-> para que el editor de código liste las funciones miembro y las variables miembro del objeto corriente, opcionalmente usted puede seguir escribiendo algunos caracteres para reducir las opciones de la búsqueda, cuando esté listo presione la tecla de Enter para ahorrarse algo de la escritura. En C# y en Java, usted puede empezar a teclear this.

Fuera del archivo o de los archivos de la clase, usted puede usar el operador de punto para listar los miembros de la clase.

IntelligentSense

© Copyright 2000-2021 Wintempla selo. All Rights Reserved. Jul 22 2021. Home