9.10 Pass by address (part 2)

주홍영·2022년 3월 13일
0

Learncpp.com

목록 보기
101/199

https://www.learncpp.com/cpp-tutorial/pass-by-address-part-2/

Pass by address… by reference?

#include <iostream>

void nullify(int*& refptr) // refptr is now a reference to a pointer
{
    refptr = nullptr; // Make the function parameter a null pointer
}

int main()
{
    int x{ 5 };
    int* ptr{ &x }; // ptr points to x

    std::cout << "ptr is " << (ptr ? "non-null\n" : "null\n");

    nullify(ptr);

    std::cout << "ptr is " << (ptr ? "non-null\n" : "null\n");
    return 0;
}

즉, 포인터의 pass by ref인 것이다
copy를 하지 않고 넘겨 받은 pointer의 alias라고 생각하면 될 것 이다

참고로 순서는 int 포인터 타입의 & ref 라고 생각하면 쉽게 외울 수 있다
int
&가 맞는 표현. 순서를 뒤바꾸면 compile error가 발생한다

profile
청룡동거주민

0개의 댓글