利用C語言實(shí)踐OOP,以及new,delete的深入分析
更新時間:2013年05月31日 10:34:54 作者:
本篇文章是對用C語言實(shí)踐OOP,new,delete進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
研究了一下,總算勉強(qiáng)能夠融會貫通了
c寫成OOP還蠻有樂趣的
編譯環(huán)境:Xcode3.2.3+gcc4.2
#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
#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);
}
// --------------------------//
#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;
}
c寫成OOP還蠻有樂趣的
編譯環(huán)境:Xcode3.2.3+gcc4.2
復(fù)制代碼 代碼如下:
#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
復(fù)制代碼 代碼如下:
#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);
}
// --------------------------//
復(fù)制代碼 代碼如下:
#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;
}
相關(guān)文章
C++?折疊參數(shù)包詳解(悄然增強(qiáng)編程效率)
折疊參數(shù)就是一個參數(shù)包, 代表是多個未知,tuple元組就是一個折疊參數(shù)的使用,這篇文章主要介紹了C++?折疊參數(shù)包悄然增強(qiáng)編程效率,需要的朋友可以參考下2023-05-05c語言實(shí)現(xiàn)兩個值互相交換的函數(shù)
本文通過代碼給大家介紹c語言實(shí)現(xiàn)兩個值互相交換的函數(shù),通過實(shí)例代碼給大家講解的很詳細(xì),具有一定的參考借鑒價值,對c語言兩個值互換函數(shù)相關(guān)知識感興趣的朋友一起看看吧2021-05-05C++實(shí)現(xiàn)LeetCode(173.二叉搜索樹迭代器)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(173.二叉搜索樹迭代器),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08C語言數(shù)據(jù)結(jié)構(gòu)算法之實(shí)現(xiàn)快速傅立葉變換
這篇文章主要介紹了C語言數(shù)據(jù)結(jié)構(gòu)算法之實(shí)現(xiàn)快速傅立葉變換的相關(guān)資料,需要的朋友可以參考下2017-06-06C++使用OpenCV實(shí)現(xiàn)證件照藍(lán)底換成白底功能(或其他顏色如紅色)詳解
這篇文章主要介紹了C++使用OpenCV實(shí)現(xiàn)證件照藍(lán)底換成白底功能(或其他顏色如紅色),結(jié)合實(shí)例形式詳細(xì)分析了OpenCV顏色轉(zhuǎn)換相關(guān)操作原理與實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-12-12C語言進(jìn)階教程之字符串&內(nèi)存函數(shù)
對于字符,在計(jì)算機(jī)內(nèi)部都是用數(shù)字(字符編碼)來表示的,而字符串是“字符連續(xù)排列”的一種表現(xiàn),這篇文章主要給大家介紹了關(guān)于C語言進(jìn)階教程之字符串&內(nèi)存函數(shù)的相關(guān)資料,需要的朋友可以參考下2021-09-09