Use C to realize C ++ Polymorphism

Source: Internet
Author: User

4. How to construct an object
We use the following structure to describe an object:
Typedef struct _ classtype
{
Char * Name;/* Object Name, unique */
Int object_size;/* object size, which is used to allocate memory when creating real columns */
Int class_size;/* all virtual function sizes, such as sizeof (cobjectclass )*/
Void * vclass;/* virtual function pointer */
Void (* initclasscallback) (void *);/* assign a value to the virtual function of the object */
Void (* initobjectcallback) (void *);/* assign initial values to instance members during Object Instantiation, which is somewhat similar to constructor */
Struct _ classtype * parent;/* parent object */
Struct _ classtype * Next;/* One-way linked list, pointing to the next object */
} Classtype;

# Define [max_class_num128
Static classtype classes [max_class_num];/* Object pool */
Static classtype * used_classes = NULL;
Static classtype * free_classes = NULL;
Only 128 objects are defined here. The number of objects may be modified or changed to dynamic allocation.

initialize, all links to the idle linked list.
void initobjectlib (void)
{< br> int I;
memset (classes, 0, sizeof (classtype) * max_class_num );
for (I = 0; I {
classes [I]. next = & (classes [I + 1]);
}< br> free_classes = & (classes [0]);
}

This function is used to obtain the virtual function pointer of an object and also to register an object.
Note that the type is static. An object is registered only once and the type is directly obtained next time.
Registerclasstype () the first parameter is the description of the object, and the second parameter is
Function pointer. Let's take a look at its definition:
# Define top_object_type 0
# Define base_object_type getobjecttype ()
Because cobject is a base object and has no parent object, the second parameter here is 0 (top_object_type ).
Now suppose there is a cobject sub-object csubobject, you should write it like this when registering it:
Type = registerclasstype (& classinfo, base_object_type );
As you can see, here is actually a recursion: When registering csuboject, The base_object_type parameter will be used,
Getobjecttype () to register the cobject.
To put it simply: If C inherits from B and B inherits from a, now suppose A and B are not registered. When C is registered, recursive
You can also register B and.

Int getobjecttype (void)
{
Static int type = 0;

If (type = 0)
{< br> static classinfo =
{< br> "object",/* Object Name, unique */
sizeof (cobject),
sizeof (cobjectclass),
(pinitobjectcallback) initobject,
(pinitclasscallback) initobjectclass,
};

Type = registerclasstype (& classinfo, top_object_type );
}

Return type;
}

Let's take a look at initobject and initobjectclass,
Static void initobjectclass (cobjectclass * vclass)
{
If (vclass = NULL)
Return;
Vclass-> destory = destoryobject;/* assign values to virtual functions */
}

Static void initobject (cobject * object)
{
If (Object = NULL)
Return;
Object-> ref_counter = 1;
}

Where are the two functions called when getobjecttype () is passed in?
Let's see registerclasstype ()
Int registerclasstype (classinfo * classinfo, int parent_type)
{
Int type = 0;
Classdesc * Current, * parent;

Parent = (classtype *) parent_type;

If (classinfo-> name = NULL)
{
Trace ("registerclasstype (): Class Name Is null \ n ");
Return type;
}
/* Check whether there are repeated object names */
Current = used_classes;
While (current)
{
If (strcmp (current-> name, classinfo-> name) = 0)
{
Trace ("registerclasstype (): Class Name is redefined \ n ");
Unlock ();
Return type;
}
Current = Current-> next;
}

/* Create an object */
Current = free_classes;
If (current)
{
Free_classes = free_classes-> next;
Current-> name = classinfo-> name;
Current-> parent = parent;
Current-> class_size = classinfo-> class_size;
Current-> initclasscallback = classinfo-> initclasscallback;
Current-> initobjectcallback = classinfo-> initobjectcallback;
Current-> vclass = malloc (current-> class_size );
If (current-> vclass = NULL)
{
Trace ("registerclasstype (): malloc vclass failed \ n ");
}
Memset (current-> vclass, 0, current-> class_size );
Current-> vclass-> class_type = (INT) current;
If (parent)/* virtual functions of sub-objects all point to virtual functions of the parent object */
{
Memcpy (current-> vclass, parent-> vclass, parent-> class_size );
}
Assign values to the new virtual functions added to the child object, or assign values to the virtual functions inherited from the parent object.
If (current-> initclasscallback)
Current-> initclasscallback (current-> vclass );
Link to object pool
Current-> next = used_classes;
Used_classes = current;
}
Else
{
Trace ("registerclasstype (): error, no class in class pool \ n ");
}

Type = (INT) Current; Note: type is a pointer of the classtype type.
Return type;
}


5. Create an instance of an object

Cobject * newobject (void)/* Create an instance */
{
Return newclasstype (base_object_type );
}

Void * newclasstype (INT class_type)
{
Classtype * Pclass = NULL;
Void * object = NULL;

If (Pclass = 0)
Return NULL;
 
Pclass = (classtype *) class_type;
/* Allocate memory space for the instance */
Object = (cobject *) malloc (Pclass-> object_size );
If (object)
{
/* Virtual function pointer of the Instance pointing to its object */
(Cobject *) object)-> vclass = Pclass-> vclass;
/* Call the initialization function of the parent object. This function is a recursive function,
First initialize the base object, then initialize the sub-object, and then initialize the sub-object ...*/
Initparentobject (Pclass, object );
}

Return object;
}

Static void initparentobject (classtype * Current, void * object)
{
If (current)
{
If (current-> parent)
Initparentobject (current-> parent, object );
If (current-> initobjectcallback)
Current-> initobjectcallback (object );
}

}

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.