C山寨C++

#include <stdio.h>
#include <string.h>
#include <malloc.h>

typedef struct Aclass_s Aclass;

struct Aclass_s
{
    int     m_iMember1;
    char    m_sMember2[40];
    void    (*m_funcInit)(Aclass *objInit);/*初始化函数*/
    int     (*m_funcPrint)(int,char *);/*成员函数*/
};



int Print(int iParam, char *sParam)
{
    printf("iParam = [%d]\n",iParam);
    printf("m_sMember2 before =[%s]\n",sParam);
    strcpy(sParam,"i am change");
    return iParam+6;
}

void Init(Aclass *objInit)
{
    objInit->m_iMember1=0;
    strcpy(objInit->m_sMember2,"init string");
    objInit->m_funcPrint=&Print;
}

int main()
{
    Aclass *AObj;
    AObj=(Aclass *)malloc(sizeof(Aclass));
    /*以下两行完成初始化*/
    AObj->m_funcInit=&Init;
    AObj->m_funcInit(AObj);
    int iTemp = AObj->m_funcPrint(AObj->m_iMember1,AObj->m_sMember2);

    printf("iTemp=[%d]\n",iTemp);
    printf("m_sMember2 after = [%s]\n",AObj->m_sMember2);

    return 0;
}


/*gcc和vc6编译通过*/

收藏于 2012-07-12

posted on 2015-06-01 15:17  阮減显  阅读(59)  评论(0编辑  收藏  举报

导航