Article title: GNOMEnclature: Prepare for GNOME2 (2 ). Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Use C for OOP programming?
In this article, we will study the Glib object system, also known as "GObject", until recently it was still part of GTK +. But before studying this new object system in Glib 2.0, we need to solve a more basic problem-what is "object system" and why does it exist? After all, C is a non-object-oriented language. Is it possible to use C to write object-oriented programs, or must C ++ be used to write object-oriented programs?
The answer is that it is possible to write object-oriented programs in C. However, because the object concept does not belong to the C language specification, an external library is needed to provide support in this regard. In this article, we use the term "object system" to describe a library that provides the basics required for OOP programming, and Glib is an example of this library. Glib provides class, inheritance, reference count, signal, interface, and object features in C. By using Glib, C programmers can easily write object-oriented programs.
Therefore, it is possible to write object-oriented programs in C. However, you may wonder why GTK + developers do not directly use C ++. Here we will not discuss every possible explanation, but just explain why it makes sense to have an object system for C. First, many developers prefer C to C ++. In addition, the C ++ compiler may not be used due to project or platform restrictions. For whatever reason, we would like to thank you for having an object system for C that allows more potential developers to do OOP programming (especially GNOME programming.
C ++ package
In this case, all C ++ supporters do not have to worry-you can also use C ++ to write GNOME programs. Because C ++ is a superset of C language, you can easily combine the C-style Glib/GTK + code with the existing C ++ project. You can also use the Glib/GTK + C ++ package. The Glib/GTK + C ++ package allows you to use the local C ++ class and object to interact with the Glib object.
Research on GObject
Now you know the meaning and purpose of the object system. Now, we will thoroughly study the Glib object system and explore some basic GObject programming concepts. Until recently, the GObject system was implemented in the GTK + 1.2.x Library. As GTK + 2 is about to be released, the object system has moved down to the layer of the new Glib 2 Library. Due to this movement, the Glib object system has nothing to do with GUI-related issues. For those of us who like to use classes and objects in non-GTK +-based programs (such as console-based applications and tools), it is a good message.
Now, let's compare the Glib Object System (also known as "GObject") with the object system in C ++ and Java. First, let's compare the syntax. With C ++, you can call methods through object pointers, as shown below:
Object-> function (arg_a, arg_ B );
By referencing a C ++ object, you can enter:
Object. function (arg_a, arg_ B );
Java only supports references and no pointers. The Java method call syntax is exactly the same as that of the C ++ reference object method call:
Object. function (arg_a, arg_ B );
In contrast, GObject uses the standard C function call syntax. Its object pointer is passed as the first parameter of the function. To prevent namespace conflicts, use the class name as the prefix:
Classname_function (object, arg_a, arg_ B );
You may be confused: what is the difference between calling the GObject method and calling a simple C function? Well, from the perspective of C programming language itself, there is no difference. For the C compiler, it only calls a function, and the first parameter of the function is a pointer to the standard C structure.
Therefore, your C compiler does not even know that we are writing object-oriented programs. However, don't let this fact fool you into thinking that GObject OOP programming only changes the good old-style C programming. GObject does a lot of work behind the scenes, allowing you to create subclasses of existing classes, create class interfaces (we will discuss this later in this article), and so on. However, all the functions of this OOP are designed to be fully compatible with the standard C programming structure.
For GObject method calling, another important thing is described here. When (when you call a method) passing an object to a class function, you need to convert the data type of the object to match the expected type of the function you are calling. For example, if you want to pass the GtkButton object to a function with the GtkWidget parameter, you need to write:
Gtk_widget_show (GTK_WIDGET (button ));
GObject interface
In addition to many improvements to GObject, Glib 2 also introduces the OOP concept called "interface. To understand what an interface is, let's look at an example.
Suppose we want to create a Pegasus object for the application. Since pegasus is a mythical horse, the habit of OOP programming is to create a new Pegasus class, which uses the Horse class as its parent class. Then, we will add the necessary code to the new class to support its "wings" feature. In this example, it makes sense to create Pegasus as a subclass of Horse because it represents the relationship between the two classes.
However, if we have a variety of unrelated classes, such as Horse, Car, and House, and we want them to communicate with each other. This new capability has nothing to do with their relationships-in fact, these three classes are basically irrelevant to us. But we hope they all support new communication-related features. What should we do?
The interface provides a solution to this problem. it allows us to add public functions to all the heterogeneous products. Back to the above example, we only need to write the "Talk" interface for the Horse, Car, and House classes. All these three irrelevant classes can communicate with each other and use the new communication-related functions we have created. In addition, these new communication-related functions use the "Talk" interface to interact happily with our objects. Therefore, because of the interface, these three irrelevant classes are now "speaking the same language ".
In Glib, you can create any number of interfaces for a class. Therefore, if we create a Talk interface for the House class, we can define the say () function as follows:
Void say (Talk * mytalk, const char * myphrase );
Then, we can call the say () function to convert the data type of the myhouse variable to the "Talk" interface.
Say (TALK (myhouse), "hello there! ");
You can find more credible interface examples in the GtkEditable interface of GTK + 2 (see references for links ). The text widget and entry widget both implement this interface.
GObject signal
Generally, the event-driven GUI program contains a main loop. In this loop, the program waits for new messages from X server. These messages (called events) are interpreted by the program and allow the program to respond to operations such as user-selected menu items and clicking buttons.
Signals are similar to events except connecting objects. They allow objects to automatically "respond" to changes in the state of another object without explicit event loops ". You only need to connect the signal of one object to the method of another object. Then, when the first object "sends" a signal (due to internal state changes), the second object "captures" the change and responds appropriately. The event loop is not required because the signal is implemented by callback-the result of sending the signal is only to call a C function. Signals are an effective and flexible "adhesive" that "binds" objects in the program.
In fact, if you have read previous GNOMEnclature columns, you will see a lot of sample code lines similar to the following:
G_signal_connect (G_OBJECT (window), "destroy", gtk_main_quit, NULL );
In the above code segment, we connect the "destroy" signal of the main window object to the gtk_main_quit () function. Due to g_signal_connect (), when our window sends a "destroy" signal (when this window is closed), our program will automatically exit. If you have written an event-driven program in the past, I think you will find that using signals is a refreshing change for setting an explicit event processing loop.
In this way, we learned how to connect signals to standard C functions. But how can we connect the same signal to a specific object? Simple-use the following template:
G_signal_connect (G_OBJECT (window), "destroy", classname_function, object );
Replace classname_function with the method to be called, and replace the object with the object pointer to be operated by the method. For more information about signals, please refer to the references section of this article for some good links.
However, note that Glib and GTK + 2.0 have made some changes to the signal. Although the basic concepts have not been changed, the signal system has been migrated from GTK + 2.0 to Glib. This produces a convenient side effect: signals can be used in non-GUI applications. If you are writing a new GNOME 2.0 code, you should use the new GSignal class of Glib. The GTK + 2.0 signal still exists, but it only contains the "package" of GSignal that uses Glib in the background ".
Despite all these changes, the Glib signal (prefix with g_signal _) is generally very similar to the signal corresponding to GTK + (prefix with gtk_signal ). However, if you have created or are planning to create your own custom signal, read the GObject GSignal Reference Document (see references) to learn about some of the changes that may apply to you.
Conclusion
Now that we have studied the basic knowledge of GObject, you should have mastered the necessary basic concepts to start your GNOME programming career. We will see you again in the next "GNOMEnclature" column and continue preparing for the GNOME 2 platform. Goodbye!
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