• Jueves 2 de Mayo de 2024, 04:38

Autor Tema:  Puntero this  (Leído 1544 veces)

player_

  • Miembro activo
  • **
  • Mensajes: 51
    • Ver Perfil
Puntero this
« en: Miércoles 6 de Octubre de 2010, 23:10 »
0
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

  • Moderador
  • ******
  • Mensajes: 796
  • Nacionalidad: mx
    • Ver Perfil
Re: Puntero this
« Respuesta #1 en: Jueves 7 de Octubre de 2010, 00:20 »
0
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*.

For instance
Código: C++
  1.  
  2. class T
  3. {
  4. public:
  5.   void foo(int a);
  6.   int goo() const;
  7. };
  8.  
  9.  
is actually:

Código: C++
  1.  
  2. class T
  3. {
  4. public:
  5.   void foo(T* this , int a);
  6.   int goo(const T* this) const;
  7. };
  8.  
  9.  

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++
  1.  
  2. UINT ThreadFunction(LPVOID param);
  3.  
  4.  

because that in fact the prototype (when non-static) is

Código: C++
  1.  
  2. UINT ThreadFunction(T* this, LPVOID param);
  3.  
  4.  

NOTA:
==================================================================
Este foro es para ayudar, aprender, compartir... usenlo para eso,
NO SE RESUELVEN DUDAS POR MENSAJE PRIVADO Y MENOS POR CORREO
==================================================================

player_

  • Miembro activo
  • **
  • Mensajes: 51
    • Ver Perfil
Re: Puntero this
« Respuesta #2 en: Jueves 7 de Octubre de 2010, 00:37 »
0
Ok, entonces si he entendido bien, en mi ejemplo, la segunda opción sería la válida:
Código: C++
  1.  
  2. void MiClase::GetX(const MiClase *this) const
  3. {
  4.  
  5. }
  6.  
  7.  


y en el otro método:

Código: C++
  1.  
  2. void MiClase::SetX(MiClase *this, int x)
  3. {
  4.  
  5. }
  6.  
  7.  

Gracias.