I've seen the concept of 'opaque types' thrown around a bit but I really haven't found a succinct answer as to what defines an opaque type in C and more importantly what problems they allow us to solve with their existence. Thanks
我已经看到了“不透明类型”的概念,但我真的没有找到一个简洁的答案,关于什么定义了C中的opaque类型,更重要的是它们允许我们用它们存在来解决的问题。谢谢
3 个解决方案
#1
29
It is the most generally used for library purpose. The main principe behind Opaque type in c is to use data though its pointer in order to hide data handling implementation. Since the implementation is hidden, you can modify the library without recompiling any program which depend on it (if the interface is respected)
它最常用于图书馆目的。 C语言中Opaque类型背后的主要原则是通过其指针使用数据以隐藏数据处理实现。由于隐藏了实现,您可以修改库而无需重新编译任何依赖它的程序(如果遵循该接口)
eg: version 1:
例如:版本1:
// header file
struct s;
int s_init(struct s **x);
int s_f(struct s *x);
int s_g(struct s *x);
// source file
struct s { int x; }
int s_init(struct s **x) { *x = malloc(...); }
int s_f(..) { ... }
int s_g(..) { ... }
version 2
版本2
// header file
struct s;
int s_init(struct s **x);
int s_f(struct s *x);
int s_g(struct s *x);
// source file
struct s { int y; int x; }
int s_init(struct s **x) { *x = malloc(...); }
int s_f(..) { ... }
int s_g(..) { ... }
From your program side, nothing changed! and as said previously, no need to recompile every single program which rely on it.
在你的程序方面,没有任何改变!并且如前所述,无需重新编译依赖它的每个程序。
#2
12
In my understanding, opaque types are those which allow you to hold a handle (i.e., a pointer) to an structure, but not modify or view its contents directly (if you are allowed to at all, you do so through helper functions which understand the internal structure).
在我的理解中,opaque类型是那些允许你持有句柄(即指针)到结构,但不能直接修改或查看其内容的类型(如果你完全允许,你可以通过理解的帮助函数来实现)内部结构)。
Opaque types are, in part, a way to make C more object-oriented. They allow encapsulation, so that the internal details of a type can change--or be implemented differently in different platforms/situations--without the code that uses it having to change.
不透明类型在某种程度上是使C更加面向对象的一种方式。它们允许封装,因此类型的内部细节可以改变 - 或者在不同平台/情况下以不同方式实现 - 而不使用它的代码必须改变。
#3
8
An opaque type is a type which is exposed in APIs via a pointer but never concretely defined.
opaque类型是通过指针在API中公开但从未具体定义的类型。
#1
29
It is the most generally used for library purpose. The main principe behind Opaque type in c is to use data though its pointer in order to hide data handling implementation. Since the implementation is hidden, you can modify the library without recompiling any program which depend on it (if the interface is respected)
它最常用于图书馆目的。 C语言中Opaque类型背后的主要原则是通过其指针使用数据以隐藏数据处理实现。由于隐藏了实现,您可以修改库而无需重新编译任何依赖它的程序(如果遵循该接口)
eg: version 1:
例如:版本1:
// header file
struct s;
int s_init(struct s **x);
int s_f(struct s *x);
int s_g(struct s *x);
// source file
struct s { int x; }
int s_init(struct s **x) { *x = malloc(...); }
int s_f(..) { ... }
int s_g(..) { ... }
version 2
版本2
// header file
struct s;
int s_init(struct s **x);
int s_f(struct s *x);
int s_g(struct s *x);
// source file
struct s { int y; int x; }
int s_init(struct s **x) { *x = malloc(...); }
int s_f(..) { ... }
int s_g(..) { ... }
From your program side, nothing changed! and as said previously, no need to recompile every single program which rely on it.
在你的程序方面,没有任何改变!并且如前所述,无需重新编译依赖它的每个程序。
#2
12
In my understanding, opaque types are those which allow you to hold a handle (i.e., a pointer) to an structure, but not modify or view its contents directly (if you are allowed to at all, you do so through helper functions which understand the internal structure).
在我的理解中,opaque类型是那些允许你持有句柄(即指针)到结构,但不能直接修改或查看其内容的类型(如果你完全允许,你可以通过理解的帮助函数来实现)内部结构)。
Opaque types are, in part, a way to make C more object-oriented. They allow encapsulation, so that the internal details of a type can change--or be implemented differently in different platforms/situations--without the code that uses it having to change.
不透明类型在某种程度上是使C更加面向对象的一种方式。它们允许封装,因此类型的内部细节可以改变 - 或者在不同平台/情况下以不同方式实现 - 而不使用它的代码必须改变。
#3
8
An opaque type is a type which is exposed in APIs via a pointer but never concretely defined.
opaque类型是通过指针在API中公开但从未具体定义的类型。