利用C語言實(shí)踐OOP,以及new,delete的深入分析
更新時(shí)間:2013年05月31日 10:34:54 作者:
本篇文章是對(duì)用C語言實(shí)踐OOP,new,delete進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
研究了一下,總算勉強(qiáng)能夠融會(huì)貫通了
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++類與對(duì)象深入之運(yùn)算符重載與const及初始化列表詳解
運(yùn)算符是程序中最最常見的操作,例如對(duì)于內(nèi)置類型的賦值我們直接使用=賦值即可,因?yàn)檫@些編譯器已經(jīng)幫我們做好了,但是對(duì)象的賦值呢?能直接賦值嗎2022-06-06
C++中實(shí)現(xiàn)矩陣的加法和乘法實(shí)例
這篇文章主要介紹了C++中實(shí)現(xiàn)矩陣的加法和乘法實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-03-03
VSCode配置C/C++并添加非工作區(qū)頭文件的方法
這篇文章主要介紹了VSCode配置C/C++并添加非工作區(qū)頭文件的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
如何在Qt中實(shí)現(xiàn)關(guān)于Json?的操作
JSON是一種輕量級(jí)數(shù)據(jù)交換格式,常用于客戶端和服務(wù)端的數(shù)據(jù)交互,不依賴于編程語言,在很多編程語言中都可以使用JSON,這篇文章主要介紹了在Qt中實(shí)現(xiàn)關(guān)于Json的操作,需要的朋友可以參考下2023-08-08
C++中函數(shù)模板與類模板的簡單使用及區(qū)別介紹
這篇文章介紹了C++中的模板機(jī)制,包括函數(shù)模板和類模板的概念、語法和實(shí)際應(yīng)用,函數(shù)模板通過類型參數(shù)實(shí)現(xiàn)泛型操作,而類模板允許創(chuàng)建可處理多種數(shù)據(jù)類型的類,文章還討論了模板的關(guān)鍵區(qū)別、注意事項(xiàng)以及它們在實(shí)際編程中的應(yīng)用,感興趣的朋友一起看看吧2025-03-03

