简体   繁体   English

我可以有一个 ELI5 用于引用和指针以及何时使用它们吗?

[英]Can I have an ELI5 for references and pointers and when to use them?

I've been learning coding for quite a while now but still cannot understand References and Pointers.我已经学习编码很长一段时间了,但仍然无法理解引用和指针。 Every answer I've searched is way too complicated for me.我搜索的每个答案对我来说都太复杂了。

When we're calling a variable to be used, for example例如,当我们调用要使用的变量时

int32 y = 1;
int32 x = 2;
int32 z = y + x;

1) What exactly is happening in "z"? 1)“z”到底发生了什么? Is it calling "y" and "x" by reference, by pointer or just calling them by variable?它是通过引用、指针调用“y”和“x”还是仅通过变量调用它们?

In this code I'm currently learning在这段代码中我目前正在学习

FString Log = TEXT("Hello");
FString* PrtLog = &Log;
Log.Len();
(*PrtLog).Len();
PrtLog->Len();

2) What is going on here? 2)这里发生了什么? Is "PrtLog" a reference or a pointer? “PrtLog”是引用还是指针?

3) The lecturer said *PrtLog is "dereferencing" PrtLog. 3) 讲师说 *PrtLog 是“取消引用”PrtLog。 Does that mean the reference for PrtLog is removed?这是否意味着对 PrtLog 的引用被删除了? Whats the difference between * and -> * 和 -> 有什么区别

4) Why do we even need a reference or a pointer if calling a variable is just as fine? 4)如果调用变量也一样好,为什么我们甚至需要引用或指针?

5) Why do some people claim 90% of variables will be using references and pointers in higher levels? 5) 为什么有些人声称 90% 的变量将使用更高级别的引用和指针? Are they beneficial in any way?它们有什么好处吗? If we just call by variable, isn't it simpler and faster?如果我们只是通过变量调用,是不是更简单更快?

Sorry if this is too many questions.对不起,如果这是太多的问题。 I can't get an answer I'm able to understand anywhere on references and pointers so I'm really confused.我无法得到一个我能够在引用和指针的任何地方理解的答案,所以我真的很困惑。

1) What exactly is happening in "z"? 1)“z”到底发生了什么? Is it calling "y" and "x" by reference, by pointer or just calling them by variable?它是通过引用、指针调用“y”和“x”还是仅通过变量调用它们?

None of those variables are pointer or references.这些变量都不是指针或引用。 They're just..variables.它们只是..变量。 x and y are used variables. xy是使用的变量。 operator+(x, y) . operator+(x, y)

2) What is going on here? 2)这里发生了什么? Is "PrtLog" a reference or a pointer? “PrtLog”是引用还是指针?

PrtLog is a pointer, you can see this by looking at its type declaration: PrtLog是一个指针,你可以通过查看它的类型声明来看到这一点:

FString*

Clearly it's a pointer to a FString .显然它是一个指向FString的指针。

The confusion might arise because of the = &Log;由于= &Log;可能会出现混淆。 . . In this part of the code & is the address-of operator required to get a pointer to Log .在这部分代码中&是获取Log指针所需的地址运算符。 & only means reference when it's part of a type, Log here is a variable, not a type. &只有当它是类型的一部分时才表示引用,这里的Log是一个变量,而不是一个类型。

3) The lecturer said *PrtLog is "dereferencing" PrtLog. 3) 讲师说 *PrtLog 是“取消引用”PrtLog。 Does that mean the reference for PrtLog is removed?这是否意味着对 PrtLog 的引用被删除了? Whats the difference between * and -> * 和 -> 有什么区别

Dereferencing is just an unfortunate name in this case, it means to get the "thing" that the pointer is pointing to.在这种情况下,取消引用只是一个不幸的名称,它意味着获取指针指向的“事物”。 A FString* is a pointer pointing to a FString so dereferencing such a pointer would yield a FString . FString*是指向FString的指针,因此取消引用此类指针将产生FString

The difference between * and -> is that -> is a shorthand for: (*pointer). *->的区别在于->(*pointer). , or "Dereference pointer and access its member`. ,或“取消引用pointer并访问其成员”。

4) Why do we even need a reference or a pointer if calling a variable is just as fine? 4)如果调用变量也一样好,为什么我们甚至需要引用或指针?

There are a few possible reasons you'd want to use a pointer or a reference.您想要使用指针或引用有几个可能的原因。 For example, to refer to an object but not copy the object itself.例如,要引用 object 但不复制 object 本身。

5) Why do some people claim 90% of variables will be using references and pointers in higher levels? 5) 为什么有些人声称 90% 的变量将使用更高级别的引用和指针? Are they beneficial in any way?它们有什么好处吗? If we just call by variable, isn't it simpler and faster?如果我们只是通过变量调用,是不是更简单更快?

Who claims this?谁声称这是? I don't have the numbers but this doesn't seem accurate.我没有数字,但这似乎不准确。 Surely they're both very useful constructs but it very much depends on the project if they're used in those numbers.当然,它们都是非常有用的构造,但是如果它们以这些数字使用,这在很大程度上取决于项目。

x and y are called lvalue expressions (usually shortened to lvalues ). xy称为左值表达式(通常缩写为lvalues )。 That means they correspond to memory locations.这意味着它们对应于 memory 位置。 The context of the expression determines whether a value is written to the memory location, or a value retrieved from the memory location.表达式的上下文确定一个值是写入 memory 位置,还是从 memory 位置检索的值。

In the code x = 2;在代码中x = 2; then a value is written to the location named by x .然后将一个值写入x命名的位置。 In the code x + 2 , a value is read from the location named by x .在代码x + 2中,从x命名的位置读取一个值。

PrtLog is a pointer because it was declared with a pointer declarator. PrtLog是一个指针,因为它是用指针声明符声明的。 The question of why someone would use pointers is answered here: Why use pointers?这里回答了为什么有人会使用指针的问题:为什么使用指针?

"dereference" means removing a level of indirection from a pointer expression. “取消引用”意味着从指针表达式中删除一个间接级别。 A pointer points to a memory location.指针指向 memory 位置。 The result of dereferencing a pointer is an lvalue expression corresponding to that memory location.取消引用指针的结果是对应于 memory 位置的左值表达式。 There can be multiple levels of this.这可以有多个级别。 a->b is equivalent to (*a).b if a is a pointer.如果a是指针,则a->b等价于(*a).b

A pointer is just a number.指针只是一个数字。 This number corresponds to where in your computer's RAM the value of the corresponding variable is stored.此数字对应于计算机 RAM 中存储相应变量的值的位置。

int theAnswer = 42;
int *pointer = &theAnswer;

std::cout << pointer << '\n';
/*
 * this will print where in memory `theAnswer` is stored,
 * it'll just look like some random number. Try it out!
 */

So in your example, (2) PrtLog is a pointer.所以在你的例子中,(2) PrtLog是一个指针。 A reference is also a pointer, but C++ sort of 'hides' that it is one.引用也是一个指针,但 C++ 有点“隐藏”它是一个。

(3) Remember that a pointer is just a number. (3) 请记住,指针只是一个数字。 So if you want to work with the pointer's value, as opposed to doing math with the pointer itself (which you shouldn't do ig), you need to somehow 'follow' that number to where the value is stored.因此,如果您想使用指针的值,而不是使用指针本身进行数学运算(您不应该这样做),您需要以某种方式“跟随”该数字到存储值的位置。 This is what dereferencing does.这就是取消引用的作用。 It 'follows' the pointer to get its value, and allows you to do things like call functions or modify it.它“跟随”指针获取其值,并允许您执行诸如调用函数或修改它之类的操作。

// make an integer variable
int theAnswer = 42;
int *pointer = &theAnswer;

std::cout << "Original: " << theAnswer << " Pointer: " << *pointer << '\n';
// prints "Original: 42 Pointer 42"
*pointer = 41;
std::cout << "Original: " << theAnswer << " Pointer: " << *pointer << '\n';
// prints "Original: 41 Pointer 41"

(4) Sometimes you just can't get access to the original, like with runtime memory allocation, or if you want to make a method to mutate a variable, etc. In c++ a lot of this is hidden in the standard library with things like references and classes, but it does still come up occasionally. (4) 有时您无法访问原始文件,例如运行时 memory 分配,或者如果您想创建一种方法来改变变量等。在 c++ 中,很多东西都隐藏在标准库中就像引用和类一样,但它仍然偶尔会出现。 Although, if you're working with raw pointers (as opposed to references or RAII classes) in C++, you should have a good reason for doing so, leaking memory is really easy.虽然,如果你在 C++ 中使用原始指针(而不是引用或 RAII 类),你应该有充分的理由这样做,泄漏 memory 真的很容易。

// you can only access this variable though the pointer
int *dynamic = new int;
*dynamic = 42;

std::cout << *dynamic << '\n';

delete dynamic;

(5) This seems to be the same question as 4, but I may be interpreting it wrong. (5) 这似乎与 4 是同一个问题,但我可能解释错了。

I hope that's clear.我希望这很清楚。 If you have any other questions feel free to comment and I'll try my best to answer them, I'd be happy to help a fellow Benjamin out!如果您有任何其他问题,请随时发表评论,我会尽力回答,我很乐意帮助本杰明同胞!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM