c++签名、指示

发布于 2024-08-27 12:29:08 字数 303 浏览 5 评论 0原文

这些签名有什么区别?


T * f(T & identifier);
T & f(T & identifier);
T f(T & identifier);

void f(T * 标识符); void f(T & 标识符); void f(T 标识符);

I met pointers in c, but the amperstand in function signature is new for me. Can Anyone explain this?

what's the difference between these signatures?


T * f(T & identifier);
T & f(T & identifier);
T f(T & identifier);

void f(T * identifier); void f(T & identifier); void f(T identifier);


I met pointers in c, but the amperstand in function signature is new for me. Can Anyone explain this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

似最初 2024-09-03 12:29:08

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

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++ 中的引用与指针即可。

An ampersand in a type declaration indicates a reference type.

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

References have many similarities with pointers, but they're easier to use and less error-prone if address arithmetic is not needed. Also, unlike pointers, references can't be rebound to 'point' to another object after their initialization. Just ask google for references vs. pointers in C++.

飘然心甜 2024-09-03 12:29:08

T * f(T & 标识符);
这是一个函数,它接受 T 的引用并返回指向 T 的指针

。 f(T & 标识符);
这是一个函数,它接受对 T 的引用并返回对 T 的引用。

T f(T &identifier);
该函数获取 T 的引用并返回 T 的副本。

void f(T * 标识符);
这个函数接受一个指向 T 的指针并且不返回任何内容。

void f(T & 标识符);
这个引用了 T 且不返回任何内容。

void f(T 标识符);
这个按值获取 T(副本)并且不返回任何内容。

引用的行为几乎与指针完全相同,只是引用永远不会被设置为 NULL,并且会为您隐式创建和取消引用引用,因此您无需在调用函数时或在函数内部处理指针语法。

T * f(T & identifier);
This is a function which takes a reference to T and returns a pointer to T.

T & f(T & identifier);
This is a function which takes a reference to T and returns a reference to T.

T f(T & identifier);
This one takes a reference to a T and returns a copy of a T.

void f(T * identifier);
This one takes a pointer to a T and returns nothing.

void f(T & identifier);
This one takes a reference to a T and returns nothing.

void f(T identifier);
This one takes a T by value (copies) and returns nothing.

References behave almost exactly like pointers except a reference is never going to be set to NULL, and a reference is implicitly created and dereferenced for you so you don't need to deal with pointer syntax while calling the function or inside the function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文