C++ when to return pointers [closed] - c++

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.

Related

How to find the total numbers of object created of my class? [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
For example, I have my class employee. I want to keep record how many employees till date have worked for me. I can make static count variable and add 1 in my constructor. But whenever my temporary object will be created when we pass object in parameters or return object of our class it will add for them too.
Static class member is the right way to go. A few things to be careful about:
Make sure you overload all constructors. The ones you don't want to support you should explicitly delete.
Don't forget to decrement in destructor.
If this program of yours is multithreaded then use atomic_uint or provide locking mechanism of your own.

Heap or stack for creating objects? [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 3 years ago.
Improve this question
I have this program which creates "Computer components" and stores it in a vector and writes the objects to a file.
If i create the objects on the stack and pass the memory address to the vector i get an error
if i create the objects on the heap and pass the pointer to the vector it works just fine "Component is a absract base class" and "CPU is a derived class from Component" Can someone explain why this is?
vector<Component*>components;
CPU x;
CPU*y = new CPU();
components.push_back(&x) // results in debug error
components.push_back(y) // works fine.
writeTofile(components);
By doing this
CPU x;
components.push_back(&x);
you create a local object on the stack and push the address of it to the vector. When the function goes out of scope, your local object is not alive anymore, and its address in the vector is invalid.

C++: why isn't the destructor designed like delete of a pointer? [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
If we delete a pointer for the first time, it release the memory and assign NULL to the pointer.
If we delete the pointer (with NULL value) for the second time, nothing happens, and no error throws out.
Then why isn't the destructor designed like delete of a pointer,
We manuall call destructor of an object, and assign something to the object, like NULL.
so that destructor can be called for many times without error?
[Update] I meant we assign NULL explicitly to the pointer.
The whole purpose of constructors and destructors is to avoid manual calling of the destructor. It's designed so that objects are automatically destroyed when no longer in use. This makes it harder for the programmer to accidentally forget to delete an object; or to use an object that has already been deleted.

Reference pointer in class [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 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.

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