Object-oriented topics for C and C + + (2)--c language can also implement object-oriented

Source: Internet
Author: User

List of articles in this column

First, what is object-oriented

Second, C language can also achieve object-oriented

Third, the non-elegant features in C + +

Iv. solve the package and avoid the interface

V. Rational use of templates to avoid code redundancy

VI, C + + can also reflect

Vii. single-Case pattern solving the construction order puzzle of static member objects and global objects

Viii. more advanced preprocessor PHP

Second, C language can also achieve object-oriented

Today we will introduce the object-oriented design method of C language, as preface above, object-oriented is a kind of thought, not a language, we'll introduce the object-oriented development method of C language implementation.

A simple and practical approach to object-oriented design of C language

As we all know, the object-oriented in C + + is very convenient, but in C, there is no class, but we can be implemented by the struct of C, and then manually pass the this pointer to the
At present, this method should be the C language design, simple and easy to use, and can better reflect the object-oriented design ideas, but unfortunately, there is no inheritance and polymorphism.

For example, a C + + class like ours

class test {public:    test(charint k) {        this->str = str;        this->k = k;    }    void ShowMessage() {        for (int0; i < k; ++i)            printf("%s\n", str);        }private:    int k;    char* str;};

Then we can convert this to a class C

/* test.h */typedefstruct _test {    int k;    char* str;} test;test* TestCreate();void ShowMessage(test*);
/* test.c */test* TestCreate() {    returnmalloc(sizeof(test));}void ShowMessage(test* t) {    int i;    for0; i < t->k; ++i) {        printf("%s\n", t->str);    }}

In fact, the idea is also very clear, easy to understand the idea, the realization is also very fresh and crisp, in all kinds of C engineering use is extremely extensive.

Complex object-oriented programming based on GObject

If you want to learn the GUI programming of C language, then it is necessary to learn the GObject class implementation way.
GObject equivalent to a C-level simulation of a C + + class object model, the implementation of course relatively more complex.

Let's actually look at a GTK window class, which is a sample of gtk+-3.0:

/ * Appwin.h * /#ifndef Appwin_h#define APPWIN_H#include "app.h"#include <gtk/gtk.h>/ * type definition and type conversion macros for this class * /#define APP_WINDOW_TYPE (App_window_get_type ())#define App_window (obj) (G_type_check_instance_cast ((obj), App_window_type, Appwindow))/ * This class is divided into two parts, part of which is the class itself.typedefstruct_appwindow Appwindow;typedefstruct_appwindowclass Appwindowclass; GType App_window_get_type (void); appwindow* app_window_new (app *app);voidApp_window_open (Appwindow *win, Gfile *file);#endif//Appwin_h

And its true definition is in the. c file

struct _AppWindow{    GtkApplicationWindow parent;};struct _AppWindowClass{    GtkApplicationWindowClass parent_class;};typedefstruct _AppWindowPrivate AppWindowPrivate;struct _AppWindowPrivate{    GSettings *settings;    GtkWidget *stack;    GtkWidget *search;    GtkWidget *searchbar;    GtkWidget *searchentry;    GtkWidget *gears;    GtkWidget *sidebar;    GtkWidget *words;    GtkWidget *lines;    GtkWidget *lines_label;};G_DEFINE_TYPE_WITH_PRIVATE(AppWindow, app_window, GTK_TYPE_APPLICATION_WINDOW);/* 后面有具体的实现方法,这里就不一一列举 */

We found that this way of defining is more advantageous than in C + + and more thorough encapsulation. Why do we say so? First of all, our declaration file is very concise, if the public method is not modified, then how to change the rest of the content, will not affect our external interface.

Secondly, because of the need to display the registration to the GObject, then dynamic class registration is possible, such a design advantage to show where? The interoperability of multiple languages is good, as many dynamic languages are supported by dynamic loading of classes and reflection loading.

In addition, Vala language is based on the GObject type, he is a new compile-time language, but it also has a lot of dynamic language features, with its development of GTK program, than C has obvious advantages.

Object-oriented topics for C and C + + (2)--c language can also implement object-oriented

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.