变量类型可以是C ++中的对象吗?

编程入门 行业动态 更新时间:2024-10-28 09:15:25
本文介绍了变量类型可以是C ++中的对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我希望我的问题清楚.我想做这样的事情:

I hope my question is clear. I would like to do something like this:

TYPE tipo = int; tipo = float;

为了以后能够做其他类似的事情:

To later be able to do other things like these:

void* ptr = new tipo(); cout << *(tipo*)ptr;

基本上,是确定空指针(或 variant -object或 any -object)的类型及其取消引用的方法,但是在执行时间.

It's, basically, to determinate the type of a void-pointer (or a variant-object, or a any-object) and its de-reference, but in execution time.

这是我的全部问题:我正在尝试创建任何类型的变量数组(为编译器模拟PC的RAM).为此,我使用了空指针和其他数组,其中我存储了一个整数,该整数表示元素的类型存储在第一个数组中.这样我就可以在需要时正确地投射元素.这段代码可以工作,但是当然不是那么有用,因为每次我必须从数组中恢复元素时,我都必须使用所有切换用例:

This is my whole problem: I'm trying to create an array of any type of variable (simulating the RAM of a PC for a compiler). For this I'm using void pointers and other array where I store an integer which represents the type of the element was stored in the first array. So then I can cast properly the element when it's required. This code works, but of course it's no so functional because I would have to use all the switch-case each time I have to recover an element from my array:

#define INT 0 #define FLOAT 1 #define BOOL 2 int main(void) { void* ram[] = { new float(2.5), new float(5.8), new bool(true), new int(8) }; int tipo[] = { FLOAT, FLOAT, BOOL, INT }; int i = 1; int salida; switch (i) { case INT: salida = *(int*)ram[i]; break; case FLOAT: salida = *(float*)ram[i]; break; case BOOL: salida = *(bool*)ram[i]; break; } cout << salida; return 86; }

我想知道我是否可以拥有类型数组",所以我可以做到这样

I wonder if I coudl have an "array of types", so I coudl do something like this

int main(void) { void* ram[] = { new float(2.5), new float(5.8), new bool(true), new int(8) }; TYPE tipo[] = { float, float, bool, int }; int i = 1; int salida = *(tipo[i]*)ram[i]; cout << salida; return 86; }

有可能吗?如果没有,您将如何解决这个问题?

Is it possible? If no, how would you solve this problem?

推荐答案

您的第一段代码基本上可以,但对性能不好,因为每个 new 分配都有相关的开销,并且您重新为数据的每个元素支付该价格.

Your first block of code is basically OK but not good for performance because there is overhead associated with every new allocation, and you're paying that price for every single element of data.

您的第二段代码实际上并不可行,至少不是您提议的那样.

Your second block of code is not really viable, at least not in the way you propose.

有一个更好的解决方案:变体向量.就您而言:

There is a better solution: a vector of variants. In your case:

std::vector<std::variant<int, float, bool>> values;

您可以在此处存储所有三种类型,如下所示:

You can store all three types in here, like this:

values.emplace_back(2.5f); values.emplace_back(5.8f); values.emplace_back(true); values.emplace_back(8);

此容器可调整大小,将您的所有数据保存在一个大的分配中(对于小型类型,这大大减少了分配开销),并且可以扩展为支持更多类型,而无需进行额外的工作.

This container is resizable, holds all your data in a single large allocation (which for small types greatly reduces allocation overhead), and can be expanded to support more types with no extra work.

参考: en.cppreference/w/cpp/utility/variant

更多推荐

变量类型可以是C ++中的对象吗?

本文发布于:2023-11-12 00:42:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580022.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   对象   类型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!