gpt4 book ai didi

C++签名,指针

转载 作者:行者123 更新时间:2023-11-30 03:11:05 24 4
gpt4 key购买 nike

这些签名之间有什么区别?


T * f(T & identifier);
T & f(T & identifier);
T f(T & identifier);<p></p>

<p>void f(T * identifier);
void f(T & identifier);
void f(T identifier);
</p>
我在 c 中遇到了指针,但函数签名中的 amperstand 对我来说是新的。谁能解释一下?

最佳答案

类型声明中的 & 表示引用类型。

int i = 4;
int& refi = i; // reference to i
int* ptri = &i; // pointer to i

refi = 6; // modifies original 'i', no explicit dereferencing necessary
*ptri = 6; // modifies through the pointer

引用与指针有很多相似之处,但如果不需要地址运算,它们更易于使用且不易出错。此外,与指针不同,引用在初始化后不能重新“指向”另一个对象。只需向 google 询问 C++ 中的引用与指针。

关于C++签名,指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2578365/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com