用C隐藏类型定义

用C隐藏类型定义

本文介绍了用C隐藏类型定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .C 带功能和定义,像这样的文件(函数库):

I have a .c file (a library of functions) with a function and a definition like this:

typedef long double big;
big foo(int x) { ... }

我要创建这个库的接口, .H 。所以,我做的:

typedef long double big;
big foo(int);

和删除的typedef长双大; .C 文件。但这样做我放弃在我的接口类型高清大,所以它不是一个真正的干净的界面。任何想法如何解决呢?

and remove the typedef long double big; from the .c file. But by doing so I give away the type definition of big in my interface, so it's not really a clean interface. Any ideas how to fix that?

我知道我可以在我的 .C 文件做到这一点:

I know I could do this in my .c file:

struct foo {
  long double;
};

然后在 .H 文件做的:

typedef struct foo big;
big foo(int);

但似乎浪费创建一个结构只是一个领域,再加上我应该使用运算符时我想读

But it seems waste to create a struct for just one field, plus I should use the . operator whenever I want to read a big.

推荐答案

如果该类型是永远不会得到比长双,那么它可能是不是更复杂值得拥有的有关隐藏更conniptions。如果可能需要变得更加复杂,那么你可以考虑使用不透明的类型。在你的公开标题, big.h ,您可以使用:

If the type is never going to get more complex than long double, then it probably isn't worth having conniptions about hiding it more. If it might need to become more complex, then you can consider using an opaque type. In your public header, big.h, you use:

#ifndef BIG_H_INCLUDED
#define BIG_H_INCLUDED
typedef struct big big_t;

extern big_t *foo(int);

...

#endif

所有的功能将采用并返回指针到 big_t 键入。这一切都可以用不完整的类型,如这一点。请注意,您的客户不能为自己分配任何 big_t 值;他们不知道的类型是有多大。这意味着你可能会与功能,如结束:

All the functions will take and return pointers to the big_t type. This is all you can do with incomplete types like that. Note that your customers cannot allocate any big_t values for themselves; they don't know how big the type is. It means you'll probably end up with functions such as:

extern big_t *big_create(void);
extern void   big_destroy(big_t *value);

创建和销毁 big_t 值。然后,他们就可以做算术:

to create and destroy big_t values. Then they'll be able to do arithmetic with:

extern big_errno_t big_add(const big_t *lhs, const big_t *rhs, big_t *result);

等。但是,由于他们只拥有一个不透明,不完整的类型,他们不能可靠地走了 big_t 结构中插科打诨。但请注意,你被限制在接口使用指针。传递或返回值需要完整的类型,如果类型齐全,用户可以考察其内部运作。

Etc. But because they only have an opaque, incomplete type, they cannot reliably go messing around inside a big_t structure. But note that you are constrained to using pointers in the interface. Passing or returning values requires complete types, and if the type is complete, users can investigate its inner workings.

在实施的头, bigimpl.h ,您将有:

In the implementation header, bigimpl.h, you'll have:

#ifndef BIGIMPL_H_INCLUDED
#define BIGIMPL_H_INCLUDED
#include "big.h"

struct big
{
    ...whatever the details actually are...
};

#endif

和您的实现code将只包括 bigimpl.h ,但包括 big.h 。这里的主要问题是确保你知道如何内存分配进行处理。

And your implementation code will only include bigimpl.h, but that includes big.h. The main issue here is making sure you know how the memory allocations are handled.

有时,这种技术是值得的。通常它是不是真的有必要。你需要为自己的评估。

Sometimes this technique is worthwhile. Often it is not really necessary. You'll need to make the assessment for yourself.

这篇关于用C隐藏类型定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 19:53