c++ - C ++中引用变量和常规变量之间的区别?
问题描述
在 C++ 中定义引用后,引用和普通变量之间有什么区别吗?
例如,在我在下面的代码中定义了一个引用之后: int x = 10; int& xRef = x;
有没有办法告诉 xRef 是对 int 的引用,而不仅仅是一个普通的 int?int& 是它自己的类型吗?
解决方案
有没有办法告诉 xRef 是对 int 的引用,而不仅仅是一个普通的 int?
您可以使用std::is_reference:
#include <iostream>
#include <type_traits>
int main ()
{
int i = 0;
int& ri = i;
if (std::is_reference <decltype (i)>::value)
std::cout << "i is a reference\n";
if (std::is_reference <decltype (ri)>::value)
std::cout << "ri is a reference\n";
}
输出:ri is a reference
所以,
int& 是它自己的类型吗?
是的。
推荐阅读
- c - 内联中file-static out-of-line、global out-of-line和no out-of-line copy函数是什么意思
- python - Dataflow Sideinputs - SDK 2.x 中的工作器缓存大小
- c# - 初始 GET 比获取 TF.exe 需要更长的时间
- reactjs - 当我滚动时,我的状态发生了变化,但在 useEffect 时不添加类
- nlp - 如何将 sklearn-DBSCAN 结果的噪声数据与其他集群相关联?
- c# - C# 中用于多个属性的数据结构,无需创建类
- kubernetes - Kubernetes API Server watch 机制是如何工作的?
- ios - 将视图内容保持在键盘 Swift IOS 上方
- angular - 从管道更改语言时如何更改日期格式
- java - 捕获错误后无法执行sql语句