references has to reference a already existed variable.
1.references不占用memery
2.when you decleare a reference ,you have to immediately assign it to something
int& ref; //error!!
3.once you declear a reference,you cannot change what it references!
#include<iostream>
#define LOG(x) std::cout<<x<<std::endl;
//pass that memery address to that function
void increment(int *value){
(*value)++;
}
int main(int argc, char const *argv[])
{
int a =5;
//increment(&a);
//LOG(a)
int& ref = a; //& is part of the type-->int& 表示int类型的reference
//ref is just an alias for a , not another variables!
std::cin.get();
return 0;
}
int& ref = a; //& is part of the type-->int& 表示int类型的reference
//ref is just an alias for a , not another variables!
本文深入解析C++中引用(reference)的概念,强调其不占用额外内存的特点,并解释了声明引用时必须立即赋值的要求。同时,文章指出引用一旦声明,不能改变其所引用的对象,通过实例演示了引用作为别名而非独立变量的特性。
1298

被折叠的 条评论
为什么被折叠?



