c - Adding const-ness to opaque handle -
if have created c module presents handle user pointer forward declared struct, so:
typedef struct foo_obj *foo_handle; if declare function prototypes use const qualified parameter thusly:
void foo_work(const foo_handle foohandle); how const-ness applied?
const struct foo_obj *foo_handle // struct foo_obj *const foo_handle // b const struct foo_obj *const foo_handle // c or ub?
b. ( there no undefined behavior code presented. )
the function call
void foo_work(const foo_handle foohandle); is equivalent
void foo_work(struct foo_obj* const foohandle); variable foohandle in function becode const pointer non-const struct foo_obj object. not able add const qualifier foohandle make pointer const object.
instead, if want have pointer const object, , keep struct hidden, must make typedef:
typedef const struct foo_obj* foo_consthandle;
Comments
Post a Comment