const指向void的目的是什么

时间:2011-10-11 12:53:07

标签: c++ c pthreads

您是否觉得奇怪pthread_setspecific需要const void指针但pthread_getspecific返回非const void指针?

void *pthread_getspecific(pthread_key_t key);
int pthread_setspecific(pthread_key_t key, const void *value);

The link

4 个答案:

答案 0 :(得分:10)

嗯,只是另一个不安全的C接口。但事实上,创建一个const - 安全版本是不可能的。如果两个函数都是一致的,那么想要存储指针的人{ - 1}}必须在存储指针时丢弃const,或者想要存储指针到非 - {{1}的人检索指针时必须抛弃const。 它的方式,它不安全,但至少它不会强迫你使用强制转换混乱代码。

答案 1 :(得分:4)

表示pthread_setspecific()未修改指向的值。您可以自由修改pthread_getspecific()返回指向的值。

答案 2 :(得分:1)

pthread_setspecific仅声明此函数不会修改value。你可以传递非常量的东西..

答案 3 :(得分:-2)

这些是getter和setter函数,其原型在许多语言中保持这样。