Using C language to practice OOP and in-depth analysis of new and delete

Source: Internet
Author: User

After studying it, I was barely able to integrate it.
Writing c into OOP is quite fun.
Compiling environment: Xcode3.2.3 + gcc4.2 Copy codeThe Code is as follows: # ifndef OBJECT_H
# Define OBJECT_H
Typedef void (* Execute) (struct Object * a_This );
Typedef struct Object * (* Allocate )();
Typedef struct _ Object_Vtable
{
Char * name;
Execute exe;
} Object_Vtable;
Typedef struct _ baseCmd
{
Object_Vtable vtable;
} BaseCmd;
// --- For common ---//
BaseCmd * new (Allocate newObj );
Void delete (void * item );
//--------------------------//
// --- For BaseCmd ---//
BaseCmd * NewBaseCmd ();
Void Exe_BaseCmd (BaseCmd * a_This );
//--------------------------//
// --- For HomeCmd ---//
Typedef struct _ homeCmd
{
Object_Vtable vtable;
} HomeCmd;
BaseCmd * NewHomeCmd ();
Void Exe_HomeCmd (HomeCmd * a_This );
//--------------------------//
# Endif

Copy codeThe Code is as follows: # include "Cloud. h"
// --- For common ---//
BaseCmd * new (Allocate newObj)
{
BaseCmd * obj = newObj ();
Return obj;
}
Void delete (void * item)
{
Free (item );
}
//--------------------------//
// --- For BaseCmd ---//
BaseCmd * NewBaseCmd ()
{
BaseCmd * cmd = malloc (sizeof (BaseCmd ));
Cmd-> vtable. name = "Cloud ";
Cmd-> vtable.exe = Exe_BaseCmd;
Return cmd;
}
Void Exe_BaseCmd (BaseCmd * a_This)
{
Printf ("do base command = % s/n", a_This-> vtable. name );
}
//--------------------------//
// --- For HomeCmd ---//
BaseCmd * NewHomeCmd ()
{
HomeCmd * cmd = malloc (sizeof (HomeCmd ));
Cmd-> vtable. name = "Home ";
Cmd-> vtable.exe = Exe_HomeCmd;
Return cmd;
}
Void Exe_HomeCmd (HomeCmd * a_This)
{
Printf ("do home command = % s/n", a_This-> vtable. name );
}
//--------------------------//

Copy codeThe Code is as follows: # include <stdio. h>
# Include <stdlib. h>
# Include "Cloud. h"
Int main (int argc, const char * argv [])
{

BaseCmd * cmd = new (NewBaseCmd );
Cmd-> vtable.exe (cmd );
Delete (cmd );

BaseCmd * cmd2 = new (NewHomeCmd );
Cmd2-> vtable.exe (cmd2 );
Delete (cmd2 );

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.