返回 type of const int &
Return type of const int &
const int z1 = 5;
const int & giveMeNumber1(){
return z1;
}
int z2 = 6;
const int & giveMeNumber2(){
return z2;
}
int main(){
int y = giveMeNumber1();
}
两个函数的返回类型似乎都是
const int
。我说的对吗?如果是这样,为什么我可以分配int y = giveMeNumber1();
?为什么这些函数没有分别返回
z1
和z2
的实际地址?当我写:int x = 3; cout << &x << endl;
输出
x
变量的地址,因此:cout << giveMeNumber1() << endl;
应该也打印地址(
const int & return type
),但是它打印的是5
。
哇,这些误解。好的,让我们看看我们能做些什么。
两个函数的返回类型似乎都是
const int
。我说的对吗?
。这两个函数的返回类型都是const int&
。参考const
int
.
如果是这样,为什么我能分配
int y = giveMeNumber1();
?
因为你要复制到y
?您可以将const
对象复制到非const
对象中。一个const
的东西不能被修改,但是复制一些东西不会修改它。
为什么这些函数没有分别返回
z1
和z2
的实际地址?
因为他们不应该。它们返回对z1
和z2
的引用。
cout << giveMeNumber1() << endl;
也应该打印地址(
const int &
返回类型)
一派胡言。你在用哪本c++书?返回类型const int&
(至少这次您做对了)表示引用。引用不是指针,也不是"地址"。我不知道你从哪里得到的概念,cout << giveMeNumber1()
会打印任何地址,但& help;它不会。
要了解更多信息,请重新阅读您的c++书籍& help;这一次,封面到封面!
这两个函数的返回类型似乎都是const int。我正确吗?如果是这样,为什么我能够分配int y = giveMeNumber1();?
不,你错了。这些函数的返回类型是const int &
。注意末尾的&符号&。它是对int的const引用。
giveMeNumber1()
返回一个左值为整型的const int &
(即z1
)。不能通过const-reference修改被引用的对象(这就是这里const
背后的含义)。但是你可以复制这个值。因此,实际情况是,引用的值被复制到y
。
同样,这些函数返回一个指向int的const引用。它们不返回地址。
- 为什么这些函数没有分别返回z1和z2的实际地址?
当我写:
int x = 3; cout << &x << endl;
打印x变量的地址,因此:
cout << giveMeNumber1() << endl;
应该也打印地址(
const int & return type
),但是它打印的是5。
似乎你对指针/地址和引用的&
的使用感到困惑。
指针将地址存储到另一个内存位置。所以你可以说它们"指向一个对象"。
引用有点像指针,但它们的行为就像对象本身。你只能用左值初始化它们,不能用像int& ref = 1
这样的文字值初始化引用。
可以对原始对象做的任何事情都可以对引用做。但是你不能重新分配一个引用,一旦它引用了一个对象,你就不能改变它,尽管有一些标准的包装器可以用来克服这个问题。
像使用对象本身一样使用引用包括获取其地址。因为你的函数返回一个引用,你也可以得到被引用对象的地址。您可以像这样编写cout
行,
cout << &giveMeNumber1() << endl; // The & here gets the address of the left value
// returned by the function.
如果你想返回变量的地址,那么你可以这样写函数,
// The returned type is a "pointer to const int", note the *
const int* giveMeNumber1() {
return &z1;
}
但是,不要将函数的返回值称为"返回其地址",而应称为"返回指针"。这样,您的原始cout行将直接打印地址
cout << giveMeNumber1() << endl;
如果你想引用指向的值,你可以写,
cout << *giveMeNumber1() << endl;
这里有一个简短、密集的例子来帮助你区分引用和指针,
int x = 16;
int* p = &x; // Pointer p initialized with the address of x
int& r = x; // r is a reference to x
const int& cr = x; // const reference to x
std::cout << x << std::endl; // x's value = 16
std::cout << &x << std::endl; // x's address
std::cout << p << std::endl; // p's value = address of x
std::cout << *p << std::endl; // int value that's in the address pointed to by p == x == 16
std::cout << &p << std::endl; // p´s address (p is a variable as well)
std::cout << r << std::endl; // x's value = 16
std::cout << &r << std::endl; // x's address (you can't get the address of the reference itself)
r = 2; // r is a reference of x, so now x = 2
cr = 2; // This won't do, you can't assign to a const reference
int y = r; // Ok, copying the value in x, 2, to y
int z = cr; // Ok, copying 2 to z
编辑:编辑以正确地解决OP的问题。
返回z1作为引用
return &z1;
实际上,您返回的是由变量z1中包含的值表示的地址。不是内存中z1的地址。
这个值可以很好地放入int中
- 'max'匹配'std::function<const int &(const int &, const int &)>'无过载
- 在C++中,如果"int a = 3; int* p = &a;",那么为什么不允许"const int* &pp = p",但允许"const int* const &pp = p"?
- 调用'myStud::myStud(int, const char [5], int, int, int)'没有匹配函数
- 返回对象时从'const DList<int>* const'到 'DList<int>*' [-fallowive] 的转换无效
- int(int)& 或 int(int) const &是什么类型?
- 传递 const int* 和 int* 时调用重载函数的不同版本(const int* const&/&&)
- 如何在构造函数中初始化int const * const
- 对"displayForStudent(int, int const*, double const*, int)"的未定义引用 collect2.exe:错误:ld 返回 1 个退出状态
- 如何在 C++ 中解释"const int *const & variable"
- 为什么"int & const" MSVC 编译得很好?
- const int const&和const int&in C++有什么区别?
- std::set<int * const> 不会编译
- " invalid operands of types 'int' and 'int* const'" 来自功能的错误,来自书
- std::initializer_list<int const> 不能从 std::initializer_list <int>构造
- 对'std::string Helper::ToString<int>(int const&)'的未定义引用
- 无法弄清楚 int[3][3] 和 int(* const)[3] 之间的区别
- const int* const fun(const int* const& p) const的含义;
- 为什么vector<int * const>是非法/不合逻辑的?
- 对 Stack<int>::p ush(int const&) 的未定义引用
- JsonCpp:未定义的对"Json::Value::operator[](int) const"的引用