Pure C implementation C ++ class

Source: Internet
Author: User

// # Include "stdafx. H"
/*
Description: pure C simulation class, pure C compiling C ++ class, simple example of pure C implementation C ++ class, structure simulation class, and struct compiling class.
C compiling class is the basis for compiling COM components in pure C.
*/
# Include <stdio. h>

Typedef struct _ vtbl
{
Void (* addref) (struct cobject * OBJ, INT); // The first parameter of all functions is similar to the hidden this pointer of class.
Void (* release) (struct cobject * OBJ );
} Vtbl; // array of function pointers

Typedef struct cobject
{
Vtbl; // a function pointer array pointing to a member function
Int;
Int B;
} Cobj; // simple definition of the structure simulation class

Void fun1 (cobj * OBJ, int count) // member function
{
OBJ-> A + = count;
Printf ("cobject: A = % d/N", obj-> );
}

Void fun2 (cobj * OBJ) // member function
{
OBJ-> --;
Printf ("cobject: A = % d/N", obj-> );
}

Static vtbl VT = {fun1, fun2}; // declare a static function pointer Array

Int main ()
{
Cobj OBJ; // define an object
OBJ. vtbl = Vt;
OBJ. A = 10;
OBJ. vtbl. addref (& OBJ, 1 );
OBJ. vtbl. Release (& OBJ );
Return 0;
}

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.