如何编写一个方法来在运行时确定 typedef 类型? [英] How can I write a method to determine the typedef type at runtime?
问题描述
我有一个二叉搜索树,我想用不同的类型实现它.二叉搜索树是模板化的,并使用以下语句确定:
I have a binary search tree that I want to implement with different types. The binary search tree is templated and is determined using the following statement:
typedef desiredType TreeItemType; // desired type of tree items i.e. string, int, Person
通常我只会将字符串树的所需类型更改为字符串,但是如果我还想创建整数树怎么办?我想我需要制定一种在运行时确定 typdef 类型的方法,但我不知道如何开始,因为 desiredType 可以是各种可变对象类型.有什么想法吗?
Normally I would just change the desiredType to string for a tree of strings but what if I want to also make a tree of integers? I'm thinking I need to make a method for determining the typdef type at runtime but I don't know to begin because desiredType can be a variety of variable object types. Any ideas?
推荐答案
很难从一行说出来,但看起来您可以使用 C 预处理器命令喜欢
It is hard to tell from one line, but it looks like you could use a C Preprocessor command like
#define saveDesiredType desiredType // save previous setting
#define desiredType char* // change to type
... <code>
#define desiredType saveDesiredType // restore previous setting
但是,我认为您只能在模块(目标文件 .o)中定义一次特定的 typedef.
however, I think you may only define a particular typedef once in a module (object file .o).
我知道在 C 中创建可行的变量类型树结构的唯一方法是转到全指针操作模型,或者将类型作为额外参数添加到树函数中,并对不同的指针进行所有的数学运算类型的大小.
稍微以对象为中心的方法是将数据封装在 tree_node 结构中带有类型数据.
The ONLY way I know to create workable tree structures of variable types in C is to go to an all pointer manipulation model, or add the type as an extra parameter to the tree functions, and do ALL the pointer math for the different sizes of the types.
A slightly more object centric approach woul be to encapsulate your data in a tree_node struct
with type data.
typedef enum D_Type { ANINT , AFLOAT, ADOUBLE, ACHAR, ASTRING, OTHER} DATATYPE;
typedef struct t_node{
DATATYPE dtype;
union { // union is ONE of the following types, and Only one.
int i; // size of the union is the size of the largest type.
float f;
double d;
char c;
char* string;
} // this union is unnamed , but could have a name.
} Tree_Node;
typedef Tree_Node* TreeItem; //pass by reference
在您的代码中,您必须打开 node->dtype 并仅使用该类型的变量.
In your code you must switch on node->dtype and work only with the variable of that type.
void tree_add (Tree T, TreeItem item)
{
int i;
float f;
double d;
char c;
char* s;
switch (item->dtype){
case ANINT:
i = item->i;
break;
case AFLOAT:
f = item->f;
break;
case ADFLOAT:
d = item->d;
break;
...
}//switch
...<code>
}//tree_add
double Pi = 3.141592653589793238;
TreeItem ti = ( TreeItem ) malloc (sizeof(Tree_Node) ); // struct Must be allocated
ti->dtype = ADOUBLE;
ti->d = Pi;
ti->s = Pi; /* OOPS! error. (might go through without being detected -- depending on compiler) */
tree_add( atree , ti);
ti->s = "Now is the time for all good programmers to come the aid of their computers.";
/* OOPS! error. undefined behavior what about the double d?
(this might go through without being detected -- depending on compiler )
but ti->dtype was not changed either so code will break. */
(看起来像工作,不是吗?)
这篇关于如何编写一个方法来在运行时确定 typedef 类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!