Programación General > C/C++

 Puntero this

(1/1)

player_:
Hola, me gustaría plantear algunas dudas acerca de lo que hace internamente el puntero this en el compilador con C++.

Por ejemplo tengo una clase cualquiera:

class MiClase
{
    private: int x;

    public:
             void GetX() const;
             void SetX(int x);
};

Imaginemos que yo creo un objeto de la clase MiClase llamado objeto.

Ahora en el main invoco al metodo GetX ----> objeto.GetX();

Pues bien, al ser el método:

void MiClase::GetX() const
{

}

el compilador internamente trata el puntero this de esta forma?

const MiClase *const this
ó de esta otra?
const MiClase *this

Creo que será de la primera forma no? Porque el puntero this siempre es constante (al hacer *const this indica que es un puntero constante que no puede cambiar de valor) y const MiClase indica que el objeto que invoca al método es de tipo MiClase y sus valores no son modificables.

Ahora al invocar al método SetX(int x) -----> objeto.SetX(5);

void MiClase::SetX(int x)
{

}

el compilador internamente trata al puntero this así?

MiClase *const this;

o así?

MiClase *this;

De nuevo pienso que será de la primera manera no? Porque volvemos a indicar que *const this es un puntero constante (no cambia de valor) y en este caso el valor del objeto que invoca al método sí se puede modificar.

A ver si estoy en lo cierto, sino os agradecería que me lo explicarais para tenerlo bien claro.

Gracias.

ProfesorX:
Informacion sacada de http://www.codeguru.com/forum/showthread.php?t=343480 y sin traducir:

Q: What is the 'this' pointer?

A: It is a misbelief that the 'this' pointer is a hidden member of a class or struct. It is a hidden parameter of non-static member functions. When you declare a function the compiler adds an extra parameter to function's prototype. The type of the parameter depends on how the function is declared. According to C++ standard, 9.3.2.1:


--- Citar ---In the body of a nonstatic member function, the keyword this is a non-lvalue expression whose value is the address of the object for which the function is called. The type of this in a member function of a class X is X*. If the member function is declared const, the type of this is const X*, if the member function is declared volatile, the type of this is volatile X*, and if the member function is declared const volatile, the type of this is const volatile X*.

--- Fin de la cita ---

For instance

--- Código: C++ --- class T{public:  void foo(int a);  int goo() const;};  is actually:


--- Código: C++ --- class T{public:  void foo(T* this , int a);  int goo(const T* this) const;};  
Static member functions, which don’t have class scope, do not have this extra parameter. One consequence is that you cannot use a non-static member function as a thread function even if it has the correct prototype


--- Código: C++ --- UINT ThreadFunction(LPVOID param);  
because that in fact the prototype (when non-static) is


--- Código: C++ --- UINT ThreadFunction(T* this, LPVOID param);  

player_:
Ok, entonces si he entendido bien, en mi ejemplo, la segunda opción sería la válida:

--- Código: C++ --- void MiClase::GetX(const MiClase *this) const{ }  

y en el otro método:


--- Código: C++ --- void MiClase::SetX(MiClase *this, int x){ }  
Gracias.

Navegación

[0] Índice de Mensajes

Ir a la versión completa