Hub Nexus
업데이트됨

작가아직 작성자가 없습니다맡기

개선할 점이 보이나요? 변경을 제안하세요.

후원

Since the C Programming-Language was not created with Object Oriented Programming in mind, it has no explicit support for classes, inheritance, polymorphism and other OO Concepts. Neither does it have its own Virtual Table, which is found in object-oriented languages such C++, Java and C#. Therefore, it might not be as easy to implement an object-oriented programming paradigm using only C's language features and standard library. However, it can be done using structures which contain both function pointers as well as data, for example, or by using third-party libraries.

There are many third-party libraries designed to add support for object-oriented programming in C. The most general-purpose and widely used among these is the GObject System, which is part of Glib. The GObject System comes with its own virtual table. To create an object in C using the GObject system, it has to be sub-classed from the GObject struct.

Object creation

In this example a new object will be implemented directly derived from GObject. For simplicity, the object is named MyObject.

Declaring an object

To create a simple non-derivable (final) object, two structs must be declared, the instance and the class. They are declared using a macro:

/* in myobject.h */
G_DECLARE_FINAL_TYPE (MyObject, my_object, MY, OBJECT, GObject)

This declares two structures, MyObject and MyObjectClass. MyObject must be defined in the C implementation, and MyObjectClass is already defined by the macro.

Boilerplate code

Since the GObject System is just a third-party library and therefore cannot make any changes to the C Language itself, creating a new object
requires a lot of boiler-plate code. This is mostly handled by the macro shown above. However, the following is also required:

/* in myobject.h */
#define MY_TYPE_OBJECT my_object_get_type ()

The macro defines several functions, namely MY_OBJECT () and MY_OBJECT_CLASS (), used for casting, MY_IS_OBJECT () and MY_IS_OBJECT_CLASS () for testing whether an object or class is of the correct type and MY_OBJECT_GET_CLASS () for getting the class structure from an instance.

Defining the object

Before use, the newly created object must be defined, along with the instance structure.

/* in myobject.c */

struct _MyObject
{
GObject parent_instance;

/* other members */
};

G_DEFINE_TYPE (MyObject, my_object, G_TYPE_OBJECT)

Static functions

There are a few static functions that may or may not to be defined, depending on your object. For a minimal object these ones are compulsory:

/* in myobject.c */
static void
my_object_class_init (MyObjectClass *klass)
{
/* code */
}

static void
my_object_init (MyObject *self)
{
/* code */
}

The constructor

There is no internal way of allocating memory for an object in C. Therefore an explicit constructor must be declared for the new object.

/* in myobject.c */

GObject *
my_object_new (void)
{
return g_object_new (MY_TYPE_OBJECT,
0);
}

Object usage

Although creating the object using its own pointer-type is perfectly valid, it is recommended to use the pointer-type of the object at the top of the hierarchy i.e the furthest off base class. The newly created object may now be used like this:

/* in main.c */

/* Note: GObject is at the top of the hierarchy. */

/* declaration and construction */
GObject *myobj = my_object_new ();

/* destruction */
g_object_unref (myobj);

Inheritance

Concept

Inheritance is one of the most widely used and useful OO Concepts. It provides an efficient way to reuse existing code by wrapping it up into an object and then sub-classing it. The new classes are known as derived classes. Many object hieriarchies can be created using inheritance. Inheritance is also one of the most efficient ways of abstracting code.

Implementation

In the GObject System, inheritance can be achieved by sub-classing GObject. Since C provides no keyword or operator for inheritance, a derived object is usually made by declaring the base instance and base class as a member of the derived instance and derived class respectively. In C code:

/* derived object instance */
struct _DerivedObject
{
/* the base instance is a member of the derived instance */
BaseObject parent_instance;
};

Further reading

Where this page came from

This page was imported from Wikibooks. From “C programming” on Wikibooks, by its contributors, under CC BY-SA 4.0. Changed here: set as a page; navigation and edit links left out; each image under its own licence, credited in its caption.

Nobody has written it yet — it is the source material at a new address, which is why search engines are asked to skip it and why no one earns from it. It is up for grabs: take it on, and it is yours to rewrite and to earn from.

이 페이지가 속한 매거진C programming

언어English

이용 허락: CC BY-SA 4.0 · 출처 en.wikibooks.org

1

0

0

0

Spinner Logo

코멘트

Spinner Logo
버전: 2CC0 1.0 — public domain
The runaway star that left the Tarantula Nebula
버전: 2CC0 1.0 — public domain
The Blackwell School, where segregation had no law behind it
버전: 2CC0 1.0 — public domain
The Eagle Nebula, seen in the infrared
버전: 2CC0 1.0 — public domain
The house where the Equal Rights Amendment was written
버전: 2CC0 1.0 — public domain
The Aleutians, the forgotten front of the Second World War
버전: 2CC0 1.0 — public domain
The Cosmic Cliffs are not cliffs