把这个类改成 c 中的结构

please change this class to C struct and fucntions!

class process 

{

    public:

    int pid;

    int b_time;

    process(int a,int b)

    {

        pid=a;

        b_time=b;

    }

    process()

    {

    }

};

how can i change the function in class in c language?

转载于:https://stackoverflow.com/questions/52961623/change-this-class-to-structure-in-c

One way is as below.

typedef struct 
{
    int pid;
    int b_time;
} ProcessType;

void processfun(int a,int b, ProcessType *Process)
{
    Process->pid=a;
    Process->b_time=b;
}

To declare a variable of the structure

ProcessType ProcessVar;

To call the function (possibly in main)

processfun(a,b,&ProcessVar);

这要看你从哪个层次上解决这个问题。
转换的目标有:
二进制兼容,你用C写出来的代码,替换了原来的,原来的编译好的程序还能直接调用,这个难度比较大。
源代码兼容,你转换以后的代码,放在整个程序里,原来的源代码,不需要修改还能编译,这个要用大量的宏展开,把构造函数嵌入进去。
功能兼容,你只要写出等价的,功能上一样的程序就可以了。这个就简单了。只要你理解c++的代码,结构体直接修改,构造、析构函数变成普通函数,在合适的时机调用即可。
性能移植,在功能兼容的基础上,保持原有程序的效率。这里不涉及。

#include "stdafx.h"

#include "stdio.h"

struct Process
{
int pid;
int b_time;
void (*Process1)(Process *p,int a,int b);
void (*Process2)(void);
};

void process1(Process *p,int a,int b)
{
p->pid=a;
p->b_time=b;
printf("p->pid=%d,p->b_time=%d\n",p->pid,p->b_time);

}

void process2(void)
{
printf("process2\n");
}

int main()
{
Process P1={0,0,process1,process2};
P1.Process1(&P1,1,2);
P1.Process2();
while(1);
return 0;
}