如何生成c++动态对象名称?

时间:2022-05-10 13:25:34

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

我想根据用户输入的数量/数字生成一些对象(c++)。

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 ?

难道没有直接使用Object1、Object2等名称的解决方案吗?ObjectX而不是拥有Classname *Object[],然后使用数组索引获取对象?

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

在这两种情况下,如果有人能澄清这个问题,那就太好了。

Thanks !

谢谢!

5 个解决方案

#1


6  

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.

到目前为止还没有人解释为什么你的想法是有缺陷的。c++是一种编译过的语言,它不遗余力地将源程序转换成高效的机器代码。由于这个原因,只有在编译时,当您将变量从源文件转换为可执行文件时,程序才可以使用您提供的变量名。之后,当您想动态创建对象时,这些信息将不再可用。程序只知道位于机器指令操作数的机器地址。

#2


8  

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::vector。无法调整内置数组的大小。如果您想通过字符串名称获取对象,那么您应该使用std:::map,它有一个索引器:

std::map<string, Classname> myMap;
myMap["Object1"] = Classname();
Classname newClassname = myMap["Object1"];

#3


3  

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

不,没有。而且,你不需要;使用std::向量。

#4


3  

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

当我9年前开始编程时,我也问了自己同样的问题。答案是:你不能。

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).

您确实可以使用一个数组并动态调整它的大小,但是使用stl向量要容易得多(一旦您学会了如何使用它)。

#5


3  

You can not do that because C++ doesn't have an "environment" (reflection) where variables (and metadata) can reside. Moreover, in C++ all variable names are vanished when the code is compiled.

A way to achieve the effect you want is to use a Map where the keys are strings.

您不能这样做,因为c++没有一个可以驻留变量(和元数据)的“环境”(反射)。此外,在c++中,当编译代码时,所有变量名都消失了。实现所需效果的一种方法是使用键为字符串的映射。

#1


6  

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.

到目前为止还没有人解释为什么你的想法是有缺陷的。c++是一种编译过的语言,它不遗余力地将源程序转换成高效的机器代码。由于这个原因,只有在编译时,当您将变量从源文件转换为可执行文件时,程序才可以使用您提供的变量名。之后,当您想动态创建对象时,这些信息将不再可用。程序只知道位于机器指令操作数的机器地址。

#2


8  

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::vector。无法调整内置数组的大小。如果您想通过字符串名称获取对象,那么您应该使用std:::map,它有一个索引器:

std::map<string, Classname> myMap;
myMap["Object1"] = Classname();
Classname newClassname = myMap["Object1"];

#3


3  

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

不,没有。而且,你不需要;使用std::向量。

#4


3  

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

当我9年前开始编程时,我也问了自己同样的问题。答案是:你不能。

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).

您确实可以使用一个数组并动态调整它的大小,但是使用stl向量要容易得多(一旦您学会了如何使用它)。

#5


3  

You can not do that because C++ doesn't have an "environment" (reflection) where variables (and metadata) can reside. Moreover, in C++ all variable names are vanished when the code is compiled.

A way to achieve the effect you want is to use a Map where the keys are strings.

您不能这样做,因为c++没有一个可以驻留变量(和元数据)的“环境”(反射)。此外,在c++中,当编译代码时,所有变量名都消失了。实现所需效果的一种方法是使用键为字符串的映射。