简体   繁体   English

我可以在C程序中使用面向对象的头文件吗

[英]can I use object oriented header file in C program

I am familiar in programming with C++ and its Object Oriented nature. 我熟悉C ++及其面向对象性质的编程。 Now I am supposed to work in C these days. 现在,我现在应该在C中工作。 I still want to use the header files I have already prepared in past which are Object Oriented(use class, constructor, destructor etc.). 我仍然想使用过去已经准备好的面向对象的头文件(使用类,构造函数,析构函数等)。 Please help me knowing about the hidden facts while doing this. 执行此操作时,请帮助我了解隐藏的事实。 Although the program doesn't give syntax erro 虽然程序没有给出语法错误

It is possible to use C for object-oriented programming. 可以使用C进行面向对象的编程。 I do this. 我做这个 But the C language does not help you. 但是C语言对您没有帮助。

I will discuss using C for object-oriented programming. 我将讨论使用C进行面向对象的编程。 But please understand I am talking about practices, not about anything built into the language. 但是请理解我在说实践,而不是在语言中内置的任何东西。

In C++, you can declare a class , and it can have public , private , and protected parts. 在C ++中,您可以声明一个class ,它可以具有publicprivateprotected部分。 In C, you can declare a struct , and everything in the struct will be public. 在C中,您可以声明一个struct ,并且该struct所有内容都是公共的。 In C++, a class can have associated functions; 在C ++中,一个class可以具有关联的功能。 in C, functions are global. 在C语言中,函数是全局的。 (In C, functions and variables declared static will be private to a single source file, but global within that file. Functions and variables not declared static are global through the whole program. There is no way to declare members of a struct to be private, although you are free to use a naming convention that makes it clear that some member variable are intended to be private.) (在C语言中,声明为static函数和变量将对单个源文件私有,但在该文件中是全局的。未声明为static函数和变量在整个程序中都是全局的。无法将struct成员声明为私有,尽管您可以随意使用一个命名约定,该约定清楚表明某些成员变量是专用的。)

In C++, you have name spaces, and a class forms a name space. 在C ++中,您具有名称空间,而一个class构成一个名称空间。 In C, there is only one global name space. 在C中,只有一个全局名称空间。

In C++, there is operator overloading; 在C ++中,存在运算符重载。 not in C. 不在C中

In C++, when you declare functions as member functions, the compiler will magically make a this pointer available. 在C ++中,当您将函数声明为成员函数时,编译器将神奇地使this指针可用。 In C, you can make member functions but you must explicitly give them an argument equivalent to a this pointer. 在C语言中,可以使成员函数生效,但是必须显式地为其赋予一个等效于this指针的参数。

In C++, the compiler will arrange to all your constructor and destructor for you. 在C ++中,编译器将为您安排所有构造函数和析构函数。 In C, you must explicitly call your functions. 在C语言中,您必须显式调用函数。

So, then, here is how to do object-oriented programming in C: 因此,这就是如何在C语言中进行面向对象的编程:

First, declare a struct to hold the data in your object. 首先,声明一个结构以将数据保存在对象中。 I recommend using a typedef so you can refer to it conveniently. 我建议使用typedef以便您可以方便地引用它。 Let's imagine you are writing a physics simulator for automobiles; 假设您正在编写用于汽车的物理模拟器; let's say your struct will be typedef ed as type CAR . 比方说,你的结构将是typedef ED类型CAR

Now you write a constructor and destructor function. 现在,您编写一个构造函数和析构函数。 Since the compiler doesn't help youit is worthwhile to make the effort to do it., you can't use a ~ in the name of the destructor. 由于编译器无济于事,因此值得付出努力。因此,不能在析构函数的名称中使用~ We will imagine your functions are declared as CAR *PcarNew(); 我们将想象您的函数被声明为CAR *PcarNew(); and void FreePcar(CAR *pcar); 并使void FreePcar(CAR *pcar); .

Now you write member functions. 现在,您编写成员函数。 A function to start the engine of the car might be void StartEnginePcar(CAR *pcar); 启动汽车引擎的功能可能为void StartEnginePcar(CAR *pcar); . (If you also have trucks, you might have a void StartEnginePtruck(TRUCK *ptruck); ; if you also have buses, you might have void StartEnginePbus(BUS *pbus); ; and so on. There is only one global namespace.) As there is no this pointer, the first argument is an explicit reference to an instance of the class (ie a pointer allocated by calling the constructor). (如果您也有卡车,则可能有一个void StartEnginePtruck(TRUCK *ptruck);如果您还有公共汽车,则有可能是void StartEnginePbus(BUS *pbus); ;等,等等。只有一个全局名称空间。)由于没有this指针,因此第一个参数是对类实例的显式引用(即,通过调用构造函数分配的指针)。

Note that in C++ you could have an object named c that was an instance of class car and you could say c.start_engine() ; 请注意,在C ++中,您可能有一个名为c的对象,它是class car的实例,并且可以说c.start_engine() ; the C++ compiler knows you mean the start_engine() member function from class car . C ++编译器知道您的意思是start_engine() class carstart_engine()成员函数。 You can have any number of functions in C++ with the same name, each a member of a different class, and C++ will use name spaces to keep track of them. 在C ++中,可以使用相同名称的任意数量的函数,每个函数都是不同类的成员,并且C ++将使用名称空间来跟踪它们。 In C you don't get anything like that, and every function name must be unique. 在C语言中,您不会得到任何类似的信息,并且每个函数名称都必须是唯一的。 This is why function names in C object-oriented programming usually have the name of the "class" in the function name itself, as in StartEnginePcar() . 这就是为什么在C面向对象编程中,函数名称通常在函数名称本身中就具有“类”的名称,就像在StartEnginePcar() (Some people would likely just use StartEngineCar() or CarStartEngine() but I use a particular coding style that encourages the style I used in my first examples. Use whatever coding style you like, of course.) (有些人可能只使用StartEngineCar()CarStartEngine()但是我使用一种特殊的编码样式来鼓励我在第一个示例中使用的样式。当然,可以使用任何喜欢的编码样式。)

(As noted in a comment below, in C you can put a function pointer inside a struct and call it with the -> operator, but this is best used for cases where you don't know in advance what the function will be, not to try to duplicate some object-oriented syntax. If you need to do something like a virtual pointer in C++, you would use a function pointer in your "class" struct .) (如下面的注释中所述,在C语言中,您可以将函数指针放在struct中,然后使用->运算符进行调用,但这最适用于事先不知道函数是什么的情况,而不是尝试复制一些面向对象的语法。如果您需要执行C ++中的virtual指针之类的操作,则可以在“类” struct使用函数指针。)

Object-oriented programming is definitely not as nice in C as in C++, but I do think that explicitly following the OOP model in C can help your programs be easier to design and to understand. 面向对象的编程在C语言中绝对不如C ++好,但是我确实认为,明确地遵循C语言中的OOP模型可以使您的程序更易于设计和理解。

C is not an Object Oriented Language. C不是面向对象的语言。

C is an imperative ( procedural ) systems implementation language. C是命令式过程式 )系统实现语言。

Assuming you have gcc compiler, try to compile with this options: 假设您有gcc编译器,请尝试使用以下选项进行编译:

gcc -std=c99 replacewithyoursourcefilename.c -o execProgram

And you will see the compiler errors. 并且您将看到编译器错误。

Is it an option to rewrite your C++ code to pure C? 是否可以将C ++代码重写为纯C语言?

I recommend you to check the C tag of StackOverflow to learn more about the language. 我建议您检查StackOverflow的C 标签以了解有关该语言的更多信息。

Hope it helps! 希望能帮助到你!

No! 没有! C standard was written before C++ standards and therefore all newer constructs give syntax errors in C. You can encapsulate C++ related things with C标准是在C ++标准之前编写的,因此所有较新的构造都在C中提供语法错误。您可以使用以下命令封装与C ++相关的内容

#ifdef __cplusplus 
..
#endif

To hide them from C compiler if this helps you to cope with compatibility issues. 如果可以帮助您解决兼容性问题,请从C编译器中隐藏它们。

I still want to use the header files I have already prepared in past which are Object Oriented(use class, constructor, destructor etc.) 我仍然想使用过去已经准备好的面向对象的头文件(使用类,构造函数,析构函数等)。

So your header files are written in C++, making use of classes, which is C++-only syntax not available in C. So the answer ist "No, you cannot use C++ classes in C". 因此,您的头文件使用类使用C ++编写,而类是C中不可用的纯C ++语法。因此答案是“不,您不能在C中使用C ++类”。

What you can do is either 您可以做的就是

  • try to encapsulate your C++ code in a library, wrap it with an API written in C. That will only work when you have a C++ compiler available on the platform you are working, but I guess you have not (why else would you bother with C in that case?) 尝试将您的C ++代码封装在一个库中,并用用C编写的API对其进行包装。只有在您正在使用的平台上可以使用C ++编译器的情况下,这种方法才有效,但是我想您还没有(为什么还要打扰一下,在这种情况下为C?)
  • rewrite your C++ classes in C, keeping an object oriented style with the C language elements. 用C重写C ++类,并使用C语言元素保持面向对象的样式。 Here is a complete online book how to do object oriented programming with C. 是一本完整的在线书籍,介绍如何使用C进行面向对象的编程。

您可以在StackOverflow中搜索“ c和c ++混合”以找到解决方案。

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

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