const 포인터와 const 참조자


자칫 잘 못 생각하면 헷갈릴 수 있음.


#include <iostream>

using namespace std;


int main(void)

{

const int num=20;

const int* ptr=&num;

const int& ref=*ptr;// int& ref=num;와 같다.

const int* (&ref2)=ptr;// ref2는 포인터 변수 ptr의 별명


    cout << "num : "<< num << endl;

cout << "*ptr : "<< *ptr << endl;

cout << "ref : "<< ref << endl;

cout << "*ref2 : "<< *ref2 << endl<<endl;


cout << "&num : "<< &num << endl;

cout << "ptr : "<< ptr << endl;

cout << "&ref : "<< &ref << endl;

cout << "ref2 : "<< ref2 << endl<<endl;


cout << "&ptr : "<< &ptr << endl;

cout << "&ref : "<< &ref << endl;

cout << "&ref2 : "<< &ref2 << endl;

}




+ Recent posts