+ 2
How to allocate memory for C++ objects professionally?
Hi Sololearners, I know that the new-operator dynamically allocates memory on the heap. But I find it difficult to apply it to real-world programs: Let's say I write a graphics-program, in which I can create objects like a square, circle, etc. with parameters. So the user creates 3 objects using "new". I can access them using a pointer and dereference. If the user deletes an object and shortens vector, the position of the objects get messed up. How can I make sure to always access the right objects?
7 Réponses
+ 2
Ah okay! I think I know what you are asking. I will just be using the console as an example, because that's what I'm most familiar with. Say, in main of my second example code I gave you, I let the user input a name and a damage of a weapon to add to the inventory. Then I would just place the variable I used for input, into the constructor. Or, I could have already created the object and could have just called a method that allows the user to input and set the values of that object. Making unique id's for objects is really easy:
class MyClass {
private:
_id;
public:
static unsigned int id;
MyClass ();
};
unsigned int MyClass::id = 0;
MyClass::MyClass () {
_id = ++id;
}
Now, every single time an object is created using the default constructor, the instance variable is assigned the value of the class variable, which is incremented every time an object of that class is created.
You could look up objects this way, but then the user would need to know the objects id. Let me work on a code specific to your question to see if I can figure this out for you!
+ 2
Here's what I have so far. Feel free to comment on it and save it as your own and send it back. I'm determined to solve your problem haha
https://code.sololearn.com/c8c2jbqJieUx/?ref=app
+ 2
I've updated my code to show searching through the vector.
Depending on what exactly you are using this for, there could be many different "professional" ways to go about this. With what you've told me, I might make my own container class that would know how to search through its own private vector variable. Kind of like the previous code I posted; it had an Inventory class, which is essentially a class that handles storing items in a vector container.
Is there any way you could post some code or are you just planning the code before you write it?
+ 1
Here is an example of how I have used the new-delete method:
https://code.sololearn.com/ckukgISgLYaw
And here is one using what most professionals will tell you how to use it. I know, there's a lot of code in there, but the smart pointer is what they refer to. In this code, I use the unique_ptr<> in the Inventory class:
https://code.sololearn.com/cS65hz5hszfv
As for your following question, "How can I make sure to always access the right objects?", I have a question of my own: Are you creating new-objects and storing them in a vector? More details about the way you are doing it will help
+ 1
Thanks a lot for your work and determination. But then again. I have to write a function that searches through the vector for the right id. Is that tha cleverest and most professional way? :)
0
Thanks for the answer, Zeke. I took a look at the first link. what you do in main() is simply creating objects with new-operator and thus assigning it a pointer, then calling a function that shows the info and then deleting the object. After that you set the pEnemy-Pointer to nullpointer restart the process for 10 times. Which is okay in my opinion.
What I would like to do is create objects and assign them values at runtime, store them in a std::vector of something (if that's what professionals do?), let the user do something else after that (change objects or delete them) and then find certain objects again (e.g. the red circle with radius 1cm). If the objects had IDs and not changing adresses, it would be easy :)
0
Thank you so much. I was actually just planning the code for a small vector graphics programm, that I'd like to write. I would like to include a graphics library to display the created objects, manipulate them and delete them later :)