Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate C++ Dynamic Objects names?

I'd like to generate a number of objects (in C++) based on the amount/number the user enters.

Now I've somewhere heard that it has to be done using pointer tricks, creating a pointer to an array of the Object type required, and then dynamically increasing the size of array ( at runtime ).

Isn't there a workaround of directly using names like Object1, Object2..... ObjectX instead of having Classname *Object[] and then using the array index to get the object ?

In either case, it'd be great if someone could clarify on the issue.

Thanks !

like image 702
suVasH..... Avatar asked Jun 20 '09 15:06

suVasH.....


People also ask

How do you create a dynamic object?

You can create custom dynamic objects by using the classes in the System. Dynamic namespace. For example, you can create an ExpandoObject and specify the members of that object at run time. You can also create your own type that inherits the DynamicObject class.

How do you create and destroy objects dynamically?

In C++, the objects can be created at run-time. C++ supports two operators new and delete to perform memory allocation and de-allocation. These types of objects are called dynamic objects. The new operator is used to create objects dynamically and the delete operator is used to delete objects dynamically.

Why do we need to do dynamic initialization of objects?

Why we need the dynamic initialization? It utilizes memory efficiently. Various initialization formats can be provided using overloaded constructors. It has the flexibility of using different formats of data at run time considering the situation.


4 Answers

If you want dynamically-sized array, then use std::vector. You won't be able to resize a built-in array. If you want to be able to get an object by string name, then you should use std::map, it has an indexer:

std::map<string, Classname> myMap;
myMap["Object1"] = Classname();
Classname newClassname = myMap["Object1"];
like image 122
Dmitry Risenberg Avatar answered Sep 21 '22 14:09

Dmitry Risenberg


So far no-one has explained why your thinking is flawed. C++ is a compiled language, and it goes to great lengths to turn the source program into efficient machine code. For this reason, the names you give variables are available to the program only at compile time, when you turn it from source into an executable file. Afterwards, when you want to create objects dynamically, those kinds of information are no longer available. The program only knows about the machine addresses where operands to machine instructions are located.

like image 29
SingleNegationElimination Avatar answered Sep 19 '22 14:09

SingleNegationElimination


No, there isn't. Moreover, you don't need to; use std::vector.

like image 39
David Seiler Avatar answered Sep 20 '22 14:09

David Seiler


When I began programming 9 years ago I asked myself the same question. The answer is: you can't.

You can indeed use an array and resize it dynamically, however using an stl vector is much easier (once you learn how to use it).

like image 21
StackedCrooked Avatar answered Sep 20 '22 14:09

StackedCrooked