this 포인터

this 포인터는 , struct또는 union 형식의 비정적 멤버 함수 내에서만 액세스할 수 있는 포인터입니다class. 멤버 함수가 호출되는 개체를 가리킵니다. 정적 멤버 함수에는 포인터가 this 없습니다.

구문

this
this->member-identifier

설명

개체의 this 포인터는 개체 자체의 일부가 아닙니다. 개체에 대한 문 결과의 sizeof 일부가 아닙니다. 비정적 멤버 함수가 개체에 대해 호출되면 컴파일러는 개체의 주소를 숨겨진 인수로 함수에 전달합니다. 예를 들어, 다음 함수 호출은

myDate.setMonth( 3 );

은 다음과 같이 해석될 수 있습니다.

setMonth( &myDate, 3 );

개체의 주소는 멤버 함수 내에서 포인터로 this 사용할 수 있습니다. 대부분의 this 포인터 사용은 암시적입니다. 의 멤버class를 참조할 때 명시적 this 을 사용하는 것은 불필요하지만 합법적입니다. 예시:

void Date::setMonth( int mn )
{
   month = mn;            // These three statements
   this->month = mn;      // are equivalent
   (*this).month = mn;
}

멤버 함수에서 현재 개체를 반환하기 위해 *this 식이 주로 사용됩니다.

return *this;

또한 포인터 this 를 사용하여 자체 참조를 방지합니다.

if (&Object != this) {
// do not execute in cases of self-reference

참고 항목

포인터는 this 수정할 수 없으므로 포인터에 this 대한 할당은 허용되지 않습니다. C++의 이전 구현에서 허용된 할당을 this.

this 경우에 따라 포인터가 직접 사용됩니다(예: 현재 개체의 주소가 필요한 자체 참조 데이터 structure를 조작하는 경우).

예시

// this_pointer.cpp
// compile with: /EHsc

#include <iostream>
#include <string.h>

using namespace std;

class Buf
{
public:
    Buf( char* szBuffer, size_t sizeOfBuffer );
    Buf& operator=( const Buf & );
    void Display() { cout << buffer << endl; }

private:
    char*   buffer;
    size_t  sizeOfBuffer;
};

Buf::Buf( char* szBuffer, size_t sizeOfBuffer )
{
    sizeOfBuffer++; // account for a NULL terminator

    buffer = new char[ sizeOfBuffer ];
    if (buffer)
    {
        strcpy_s( buffer, sizeOfBuffer, szBuffer );
        sizeOfBuffer = sizeOfBuffer;
    }
}

Buf& Buf::operator=( const Buf &otherbuf )
{
    if( &otherbuf != this )
    {
        if (buffer)
            delete [] buffer;

        sizeOfBuffer =  strlen( otherbuf.buffer ) + 1;
        buffer = new char[sizeOfBuffer];
        strcpy_s( buffer, sizeOfBuffer, otherbuf.buffer );
    }
    return *this;
}

int main()
{
    Buf myBuf( "my buffer", 10 );
    Buf yourBuf( "your buffer", 12 );

    // Display 'my buffer'
    myBuf.Display();

    // assignment operator
    myBuf = yourBuf;

    // Display 'your buffer'
    myBuf.Display();
}
my buffer
your buffer

포인터의 this 형식

포인터의 형식은 this 함수 선언에 키워드(keyword) 포함 constvolatile 되는지 여부에 따라 변경됩니다. 다음 구문에서는 멤버 함수의 this 형식을 설명합니다.

[cv-qualifier-list] class-type* const this

멤버 함수의 선언자가 결정합니다.cv-qualifier-list 그것은 또는 volatile (또는 둘 다)일 const 수 있습니다. class-type 는 .의 이름입니다 class.

포인터를 this 다시 할당할 수 없습니다. const 멤버 함수 선언에 사용되는 한 volatile 정자는 다음 표와 같이 해당 함수 범위에서 포인터가 가리키는 인스턴스 this 에 적용 class 됩니다.

멤버 함수 선언 명명된 class 포인터의 this 형식myClass
void Func() myClass *
void Func() const const myClass *
void Func() volatile volatile myClass *
void Func() const volatile const volatile myClass *

다음 표에서는 'volatile에 대해 const 자세히 설명합니다.

한정자의 this 의미 체계

한정자 의미
const 멤버 데이터를 변경할 수 없습니다. 가 아닌 멤버 함수를 호출할 수 없습니다 const.
volatile 멤버 데이터는 액세스할 때마다 메모리에서 로드됩니다. 는 특정 최적화를 사용하지 않도록 설정합니다.

개체를 그렇지 않은 멤버 함수에 전달하는 const 것은 오류입니다 const.

마찬가지로 개체를 그렇지 않은 volatile멤버 함수에 전달하는 volatile 것도 오류입니다.

멤버 데이터를 변경할 수 없음으로 const 선언된 멤버 함수입니다. 함수 this 에서 const 포인터는 개체에 대한 포인터입니다const.

참고 항목

Constructors 및 destructors를 로 또는 volatile.로 const 선언할 수 없습니다. 그러나 개체 또는 volatile 개체에서 const 호출할 수 있습니다.

참고 항목

키워드