Reference pointer in class [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Well, I need an attribute in my class is a pointer reference & and start to null, because it pointed to the address of an image that has not yet been charged. It is possible to do this?
If not possible, is as follows:
I'm creating a game with Allegro 5 and C ++, and in part to carry the images of tiles (tileset) I carry every memory and for each object of the game I pass him the reference pointer (memory address) of your respective image.
I'm doing the right thing? Got any better way?

Don't use a reference, use a pointer.
Image* image = nullptr;
image = loadImage(...);
image->getWidth();
A reference must reference an existing object, if the variable should have a "non loaded state" then you are out of luck. You must use a pointer or let it reference a non valid object created just as a placeholder.

Related

How to name a memory location (C++)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
This is a question regarding pointers in C++.
The diagrams that follow use the following representation:
Let's say we have int* k = 1778. This is represented in the left diagram below. How do I name the memory location in k? This is marked by the question mark in the diagram on the right.
I understand that if I write int x = *k, a new memory location will get assigned as shown on the right below, so this does not accomplish the task.
Variable names are not a property of the memory location. Those names are used by the program for accessing relevant memory locations. So that you cannot name a memory location in the physical memory (RAM).
If you need to access the dereference(*) of the pointer k through a new name, please create a reference variable and assign the dereference of pointer k to created reference variable.
Example:
int& ref_k = *k;

C++ when to return pointers [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Suppose you have a function that is supposed to return an object, do you generally return a pointer of the object or the object itself? When should you return a pointer/object?
Generally speaking you will probably want to return object references, since that way you are only transferring a copy of the pointer and not a copy of the entire object - especially if the object is large.
My understanding is that you'll only want to return a copy when you're creating an object on the stack within a function - this is because the object will go out of scope when the function returns and will be deleted. Objects created on the heap can be returned by reference but must be deleted later to avoid memory leaks.

Why would you use pointers for Low Memory Allocation? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
So I was looking at the source code of unreal engine and I came across this:
class USphereComponent* CollectionSphere;
There are using pointer for something that is going to be initialized and spawned once when the game begins and some objects will be overlapping it,I thought pointers are mainly used for more mid-high memory allocation purposes
So why are they using it now?
Keep in mind Im not really good at pointers so Im just trying learn and gather some information about it.
Whether to use pointers or not has almost nothing to do with "low" or "mid-high memory allocation purposes".
You use a pointer when you need a pointer, and often that's when the pointee (the thing being pointed to) will live for longer than an "automatic" object would in any of the program's existing, conveniently-accessible scopes.
Basically, *CollectionSphere will be a global (or something similar), but we can't construct it right away. The author has decided that they need fine control over the lifetime of the object. So, they have created a pointer to point to that object when the time comes.

What are jobject and jmethodID? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I wanted to know what jobect is? I know it is opaque structure. But how can we access fields of an object using an opaque structure?
When we call a function, using a function pointer (for example GetMethodID), What is ID of a method? how are we we getting the id of a method? What exactly is it's return type. I know it's jmethodID, but what is jmethodID?
I wanted to know what jobect is? I know it is opaque structure.
Correct.
But how can we access fields of an object using an opaque structure?
You can't.
When we call a function, using a function pointer (for example GetMethodID)
There is no function pointer there, except GetMethodID itself.
What is ID of a method?
It's an opaque quantum that permanently identifies the method. You don't need to know anything else about it.
how are we we getting the id of a method?
By calling GetMethodID().
What exactly is it's return type. I know it's jmethodID, but what is jmethodID?
It's whatever it says it is in jni.h. You don't have any reason for needing any more information than that.

Reference to a variable of different type [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Is it possible to create a reference of an another data-type referring to a variable of a different datatype?
LPWSTR Buffer = new WCHAR[BUFFER_LEN];
LPBYTE& rfBuffer = (LPBYTE&) Buffer;
//Compiles but rfBuffer is BAD
Yes. The syntax T x = (T&)y tells the compiler to treat the memory taken by the variable y as if a T was located there. But it's just a gross abuse of the language.
Refrences are implicit pointors. They hold the address of the original object but behave syntactically as the object itself. According to this MSDN article: Any object whose address can be converted to a given pointer type can also be converted to the analogous reference type