将结构划分为私有和公共部分? [英] Partitioning struct into private and public sections?

查看:180
本文介绍了将结构划分为私有和公共部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++和Java中,数据结构可以具有privatepublicprotected区域.我想将此概念移植到我正在编写的C语言程序中.

In C++ and Java, data structures can have private, public and protected regions. I'd like to port this concept to a C language program I am writing.

在C struct中是否有用于实现私有或受保护的函数指针和数据字段的惯用法? 我知道C struct是公共的,我正在寻找一种习惯用法,以帮助隐藏一些实现细节并强制用户使用公共接口.

Are there any idioms for implementing private or protected function pointers and data fields in a C struct? I know that C structs are public, I'm looking for an idiom to help hide some implementation details and force users to use the public interface.

注意:该语言已由商店选择,因此我坚持将面向对象的概念实现到C中.

谢谢.

推荐答案

如您所知,您不能这样做.但是,有些习惯用法也会产生类似的效果.

As you know, you cannot do this. However, there are idioms that will allow a similar effect.

C允许您执行类似于面向对象设计中的"pimpl"惯用语的操作.您的结构可以有一个指向另一个前向声明的结构的不透明指针,该结构充当该结构的私有数据.在该结构上运行的函数(代替成员函数)可以具有私有成员的完整定义,并且可以使用它,而代码的其他部分则不能.例如:

C will allow you do do something similar to what is known as the "pimpl" idiom in object-oriented design. Your struct can have an opaque pointer to another forward-declared struct that acts as the struct's private data. Functions that operate on the struct, taking the place of member functions, can have the full definition for the private member, and can make use of it, while other parts of the code cannot. For example:

在标头中,foo.h:

In a header, foo.h:

  struct FooPrivate;

  struct Foo {
     /* public: */
       int x; 
       double y;
     /* private: */
       struct FooPrivate* p;
  };

  extern struct Foo* Foo_Create(); /* "constructor" */

  extern void Foo_DoWhatever(struct Foo* foo); /* "member function" */

在实现中,foo.c:

In the implementation, foo.c:

  struct FooPrivate {
     int z;
  };

  struct Foo* Foo_Create()
  {
     struct Foo* foo = malloc(sizeof(Foo));

     foo->p = malloc(sizeof(FooPrivate));

     foo->x = 0;
     foo->y = 0;
     foo->p->z = 0;

     return foo;
  }

  void Foo_DoWhatever(struct Foo* foo) 
  {
      foo->p->z = 4; /* Can access "private" parts of foo */
  }

在程序中:

  #include "foo.h"

  int main()
  {
      struct Foo* foo = Foo_Create();

      foo->x = 100; /* Can access "public" parts of foo */
      foo->p->z = 20; /* Error! FooPrivate is not fully declared here! */

      Foo_DoWhatever(foo); /* Can call "member" function */

      return 0;
  }

请注意,需要使用构造函数"功能为私有数据分配内存.显然,您需要将其与特殊的析构函数"函数配对才能正确地解除分配私有数据.

Note the need to use a "constructor" function in order to allocate memory for the private data. Obviously you would need to pair this with a special "destructor" function in order to deallocate the private data properly.

或者,或者,如果您希望结构没有公共字段,则可以使 entire 结构不透明,而将标头设置为类似

Or, alternatively, if you would like your struct to have no public fields whatsoever, you could make the entire struct opaque, and just have the header be something like

  struct Foo;

  extern struct Foo* Foo_Create(); /* "constructor" */

  extern void Foo_DoWhatever(struct Foo* foo); /* "member function" */

在foo.c中具有struct Foo的实际定义,并且getter和setter函数可用于要直接访问的任何属性.

With the actual definition of struct Foo in foo.c, and getter and setter functions available for any properties you would like to provide direct access to.

这篇关于将结构划分为私有和公共部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆