简体   繁体   English

是否有用于在ansi C中制作抽象数据类型的标准方法?

[英]Is There a Standard Method for Making Abstract Data Types in ansi C?

I recently transferred into a different school & cs program. 我最近转到了另一所学校和CS计划。 The language used is C as compared to java which was taught at my previous school. 与我以前的学校教过的java相比,使用的语言是C。 One of my main issues which may be the result of not writing enough C code is that I'm having trouble finding a standard for making Abstract Data Types. 我的主要问题之一可能是未编写足够的C代码造成的,这是我在寻找用于制定抽象数据类型的标准方面遇到困难。

From what I've seen, there are tons of ways these are implemented and the lack of a visible standard is making me worried I missed something while self learning C. I've seen implementations that hide the init variable from the user such as 从我所看到的情况来看,实现它们的方式有很多种,缺乏可见标准使我担心我在自学C时错过了一些东西。我已经看到了对用户隐藏init变量的实现,例如

#define createVector(vec) Vector vec; void init_vector(&vec)

and another version which is what I would be more used to in which a handle is used to hold the returned pointer to struct from the createVector() function. 另一个版本是我更习惯的版本,其中使用一个句柄来保存从createVector()函数返回的指向struct的指针。 The issue is I can't find any detailed description on handles online or in my course 2 book. 问题是我在网上或课程2的书中找不到有关手柄的任何详细描述。 The course 2 book only shows the interface and methods but not how they are grouped together in a way that hides the implementation from the user. 第2本书仅显示界面和方法,而未显示如何将其分组在一起,从而对用户隐藏了实现。 I wanted to know if there was a "correct" way/standard for ADTs? 我想知道ADT是否有“正确”的方式/标准? The book in question is Robert Sedgewick "Algorithms in C - Third Edition". 有问题的书是罗伯特·塞奇威克(Robert Sedgewick)的《 C算法-第三版》。

Abstract Data Types 抽象数据类型

Split your sources. 分割您的来源。 The header (.h files) contains the abstract declarations like the datatypes (structs, functions, enums, constants, etc) The actual implementation is done in the .c files. 标头(.h文件)包含抽象声明,例如数据类型(结构,函数,枚举,常量等)。实际实现在.c文件中完成。 When using such a (lets call it) module you only include the header in your source. 当使用这样的(让它称呼它)模块时,您仅在源文件中包含标题。 The implementiation you use is decided at linking time. 您使用的实现是在链接时决定的。 You may decide to use different .c files for implementation or a static library (or even a dynamic library). 您可能决定使用不同的.c文件来实施,也可以使用静态库(甚至是动态库)。 If you want to hide the data you use opaque structures . 如果要隐藏数据,请使用不透明结构

Why is this standard? 为什么是这个标准? Ever heard of the FILE type? 听说过FILE类型吗? This is the opaque type used for IO in c's standard library. 这是c 标准库中用于IO的不透明类型。 You only include the header stdio.h and leave the implementation to the compiler. 您仅包含头文件stdio.h ,并将实现留给编译器。 The header on the other hand or at least the symbols that it defines are well documented (and part of the c standard). 另一方面,标头或至少它定义的符号有据可查(和c标准的一部分)。

Abstract Classes 抽象类

Java has the concept of an abstract class. Java具有抽象类的概念。 Well, it also has the concept of a class in general. 好吧,它也具有一般类的概念。 C does not. C没有。 This is more a personal opinion but don't waste time on emulating language features that the language does not offer . 这更多的是个人观点,但不要在模拟语言所不提供的语言功能上浪费时间 For none abstract methods use functions which take a pointer to a (probably opaque) struct containing all the data needed as first parameter, like fprintf(FILE*,const char*,...) . 对于抽象方法,没有一种方法使用函数,该函数采用指向(可能不透明)结构的指针,该结构包含作为第一个参数所需的所有数据,例如fprintf(FILE*,const char*,...) For abstract methods you will need function pointers. 对于抽象方法,您将需要函数指针。 Use these function pointers (or maybe a struct of function pointers) like a strategy . 策略一样使用这些函数指针(或者可能是函数指针的结构)。 You may define a method for registering such a strategyand delegate the normal functions to them. 您可以定义注册这种策略的method ,然后将常规功能委托给它们。 Take for example the atexit function, which globally (you may call it a singleton) adds a exiting- strategy . atexit函数为例,该函数在全局范围内(您可以将其称为单例)添加了退出策略

The XY Problem XY问题

I'm having trouble finding a standard for making Abstract Data Types 我在寻找制作抽象数据类型的标准时遇到了麻烦

Read about this and apply it to your question. 阅读有关此内容并将其应用于您的问题。 Instead of trying to force your solution to work rethink if the attempted solution is applicable to the problem. 如果尝试的解决方案适用于该问题,请重新思考,而不要强迫解决方案工作。 Try to get comfy with the techniques described above. 尝试使自己适应上述技术。 This may need a bit of practice but then you can model your solution in a more c-styled way. 这可能需要一些实践,但是您可以以更c风格的方式对解决方案建模。

I just wanted to post this as I figured out the answer that would be more specific to my case however I understand that this probably doesn't apply to everyone. 我只是想发布此信息,因为我想出了更具体的答案,但是我知道这可能并不适用于所有人。 The thing I was looking for was the idea of "First Class ADTs" which use a handle to contain a pointer to the actual object that was created from a .c implementation file that would be hidden from the user. 我要寻找的是“头等舱ADT”的概念,它使用一个句柄包含指向从.c实现文件中创建的实际对象的指针,该实现文件对于用户而言是隐藏的。

For ADT using C, this approach is the standard as far as I know. 据我所知,对于使用C的ADT,这种方法是标准的。 You will have a header (.h) file and one or more implementation (.c) files. 您将拥有一个头文件(.h)和一个或多个实现文件(.c)。 The header file might look something like: 头文件可能类似于:

typedef struct * Doodad;

Doodad * doodadInit(int);

void doodadDestroy(Doodad *);

int doodadGetData(Doodad *);

void doodadSetData(int);

For your implementation file(s) you might have: 对于您的实施文件,您可能具有:

typedef struct iDoodad {
   int data;
} Doodad;

Doodad * doodadInit(int data) {
   ...
}

...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM