C++ Reference Variable
Characteristic of "Reference Variable"
1) Have to be Initialized
int lvalue = 3;
int &ref ; // error
2) Have to be Initialized with 'l-value'
"l-value" vs "r-value"
l-value : value that has an memory assigned by OS
r-value : x ex) literal, number
int l_value = 3; // l_value having value '3' with memory assigned by OS
int &ref = l_value; // ok
int &ref = 3; // no
3) Value, Address All same with " original variable"
int l_value = 3;
int &ref = l_value;
cout << ref << l_value << endl; // same
cout << &ref << &l_value << endl; // same
4) no need to "copy" variable when used as function parameter
- Much more Effective In terms of Processing
int main()
{
int l_value = 3;
doSth(l_value)
}
// - variable 'l_value' itself is inserted as parameter
// - with no 'variable copying' process
void doSth(int &l_value)
{
cout << "ref" << ref << endl;
}
// - variable 'l_value' is copied
// address of 'l_value' in 'main' &&
// address of 'l_value' in 'doSth'
// is different , since new variable 'l_value' is generated as a result of 'copy'
void doSth(int l_value)
{
cout << "ref" << ref << endl;
}
Author And Source
이 문제에 관하여(C++ Reference Variable), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@dhsys112/C-Reference-Variable저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)