C++ Retard Here

I’m having a hard time understanding pointers/references in C++. I’m coming from a C# background, so all this shit is handled for me already in that language — but I want to learn C++.

I already know that a pointer just gets the memory address of a value, and you can print the value of that pointer’s address with &. However, I want to understand pointers and references with objects and classes, as that’s used constantly in UE C++.

What are some good resources that explain pointers and references as if I’m a total retard (which I am)?

Attached: DA39022B-7131-43F7-BB86-B72C1EDCE645.jpg (1008x1296, 92.21K)

Other urls found in this thread:

youtube.com/watch?v=IzoFn3dfsPA
github.com/jetblacksalvation/CPP-Parse-Spaces
twitter.com/AnonBabble

Bump

Reference is basically same as a pointer, except the compiler will dereference it for you automatically. And a reference can't be null but a pointer can.

A pointer is a possibly null reference to an object that exists somewhere in memory. A reference is syntactic sugar the language provides for pointers that are always non-null. The following two functions are equivalent.
int x = 1;

void foo() {
int *x_ptr = &x;
*x = 2;
}

void bar() {
int &x_ref = x;
x = 2;
}

Why use pointers in most use cases then?

Pointers can have semantics of "owning" their memory. References cannot own memory and only refer to memory that exists somewhere else. For example, `std::unique_ptr` is a "smart pointer" that owns it's memory. An extremely common semantic is something like this.

auto p = std::make_unique(1); // owns memory containing "1"
int &ref = *p; // reference to 'p' that does not control the memory.

youtube.com/watch?v=IzoFn3dfsPA

Okay, thanks. So, let’s say I wanted to make a game with a collection of “Items.” Assume they’re using some Item class.

Would it make more sense to have a vector of objects or a vector of pointers to the class? If it’s the latter, can you have a vector that points to a base class but contains classes inheriting from the base?

references are just compiler trickery, pointers point to actual memory locations and take up memory themselves

Also hoping someone can explain this in a UE context. In exchange, my bussy

It depends what you're doing with it. Objects are potentially very expensive to copy while pointers are not. You can easily use a vector to store objects and then take references to them. But if you want to do a lot of copies of that vector you're going to be copying a lot of objects. One problem with vectors is that pointers to them are not stable after inserting, so any references you had to the vector can be invalidated after appending it. If you use pointers that's not going to be an issue. Really depends on your use-case and who is going to be managing the memory.

C# has pointers and references as well. They're just not used often (see the ref, in and out keywords and also the unsafe context).

If you pass a class by reference you can straight up change shit without it being deleted after the function ends. For example, say you have a textfile and you want to seperate them by spaces, but you dont want the vector's( what you are storing the words in) data to be temporary an shit. You can either pass it as a pointer, or a reference. They are the same thing basically. If you want to see that in action look at this git hub repo github.com/jetblacksalvation/CPP-Parse-Spaces

Can you show me an example of this?

repo has example

Oldfag here. If pointers aren't immediately intuitive then I am afraid you probably don't have the IQ. I have never seen someone who had trouble with pointers "get it" later. They are literally just addresses in memory. Fun fact: we used to specifically use small pointer problems to sort people in interviews. We used to call them scriptkiddies.

Attached: ohnoitsretarded.jpg (480x161, 10.41K)

>I have never seen someone who had trouble with pointers "get it" later.
But thats literally me. I didnt get them when I was 15, but they make sense now.

Don't use raw pointers in seppeles you gorilla monkey

same here. Pointers made 0 sense in high school. Years later in my EE degree, I took a class on assembly programming and realized "shit this is just pointers", and then it clicked and made sense

I think the problem is that autistic programmers wrote all these c books, and they didn't explain it right. They also seemed to make everything seem harder and more difficult than it really was overexplaining everything.

OP here.

I do perfectly fine in other languages, like C#. C++ introduces a lot of concepts I’m not familiar with, but I’m confident I’ll get them eventually.

Don’t be such a cunt about it, everyone learns at different rates and starts out learning different things.

Tbh my biggest issue is just keeping track of everything in a large project, and pointers are frequently used in large programs (like Unreal Engine). I think I just get overwhelmed. Probably an ADHD thing