简体   繁体   English

当类型已知时,如何从void指针返回具有类型的指针

[英]How to return a pointer with a type from a void pointer, when the type is known

I'm currently making a small library which can access random.org and get random strings or integers. 我目前正在制作一个小型库,该库可以访问random.org并获取随机字符串或整数。 I've run into a slight problem now though, design wise, and I can't decide which of my two approaches that would be the best, so I will write down my thoughts about both and ask the questions that I find relevant to both. 但是,从设计角度来看,我现在遇到了一个小问题,我无法决定两种方法中哪一种最好,因此我将写下关于这两种方法的想法,并提出与这两种方法相关的问题。

I currently have a struct like this: 我目前有一个这样的结构:

typedef struct {
char **array;
size_t row;
size_t col;
size_t size; 
} MemoryStruct;

And this is the source of my "problem", as I now have added in integer handling into my library. 这就是我的“问题”的根源,因为我现在在我的库中添加了整数处理。

As it can be seen, the pointer is currently a char pointer, and as I want to be able to handle blot integers and chars, should I add another pointer - an integer pointer - or should I instead make it a void pointer and make a function that will return the correct type of pointer? 可以看出,该指针当前是一个char指针,并且由于我希望能够处理印迹整数和char,我应该添加另一个指针-整数指针-还是应该将其设置为void指针并创建一个将返回正确类型的指针的函数?

The addition of an integer pointer would be the easiest, but I'm not sure how obvious it would seem to the guy who might use my library at some point in the future, that this is why his program segfaults, because he used the wrong pointer in the struct. 添加整数指针将是最简单的,但是我不确定对于将来可能会使用我的库的人来说,这看起来是多么明显,这就是为什么他的程序存在段错误,因为他使用了错误的结构体中的指针。

However, adding in a void pointer instead means that I will have to do a context based function that will return a pointer of the correct type (if this is even possible - I quite honestly don't know if it is), which leads to the question: 但是,添加一个void指针意味着我将不得不执行一个基于上下文的函数,该函数将返回正确类型的指针(如果可能的话-我很坦白地说不知道它是否正确),这导致问题:

Which return type would a function that can return different kinds of pointers have? 可以返回不同类型指针的函数将具有哪种返回类型? (... it's obvious, a void pointer!) but then I still don't have the wanted type included for easy use for the programmer who're using my library - here is it put into pseudo code what I would like to do. (...很明显,它是一个空指针!)但是我仍然没有包含想要的类型,以便于使用我的库的程序员轻松使用-这是将其放入伪代码中我想要做的事情。

pointer-with-type-info *fun(MemoryStruct *memory, RandomSession *session)
switch case on session->type
return pointer of appropriate type

It should be added here, that from session->type it can inferred which type the pointer should be. 应该在这里添加,它可以从session-> type推断出指针应该是哪种类型。

Thank you in advance for reading this. 预先感谢您阅读本文。

The way I understood your question, you know the pointer data type. 我理解您的问题的方式,您知道指针数据类型。 I would use a void* instead of two pointers, because you may add more data types later without creating more fields. 我将使用void*而不是两个指针,因为您以后可以添加更多的数据类型而无需创建更多的字段。

To retrieve the correct value, one can create two functions: int getAsString(MemoryStruct, char** value) and int getAsInteger(MemoryStruct, int &value) . 要检索正确的值,可以创建两个函数: int getAsString(MemoryStruct, char** value)int getAsInteger(MemoryStruct, int &value) These functions would return a non-zero value in case of success ( true ). 如果成功( true ),这些函数将返回非零值。 This way, you will be able no only to retrieve the correct value, but also you will have a way to know whether the value can be retrieved at as this data type. 这样,您不仅可以检索正确的值,而且还可以知道是否可以以此数据类型检索该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM