如何使用C结构在C中实现C ++类遗产属性? [英] How to implement C++ class heritage properties in C using C struct?

查看:59
本文介绍了如何使用C结构在C中实现C ++类遗产属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,

我有一个C ++程序需要转换为c语言才能在一个电子邮件系统中使用。问题是原始代码是用C ++语言编写的,带有Parent类和一些子类的
。我怎样才能通过在C语言中使用struct将这些C ++代码转换为纯c代码?


有人可以给我任何想法吗?谢谢。


例如,如何将以下代码转换为纯c代码?


class PARENT

{

public:

virtual int Init();

virtual int Add();

virtual int Sub ();

int Otherfunc();


private:

int x;

int y;

int z;

};

class CHILD1:public PARENT

{

public:

int Init();

int Add();

int CHILDOwn();

privete:

int a;

int b;

int c;

};


class CHILD2:公共父母

{

public:

int Init();

int Add();

int Uncle();

privete:

int m;

int n;

int d;

};


PARENT * CreateNewInstance()

{

PARENT * pPointer =新CHILD1;

返回pPointer;

}


主( )
{

PARENT * Kids1 = CreateNewInstance();

Kids1-> Uncle(); //来自儿童班

Kids1-> Sub(); //来自父类

Kids1-> Otherfunc(); //来自父类

}

解决方案



的Bugzilla写道:喜,全部
我有一个C ++程序需要转换为c语言用于电子邮件系统。问题是原始代码是用C ++语言编写的,带有Parent类和一些子类。如何通过在C语言中使用struct将这些C ++代码转换为纯c代码?

有人可以给我任何想法吗?谢谢。


更好的方法是你写一个externC包装器函数在$ c $ b C ++程序中,然后将包装器和程序

编译成一个静态库。


然后你可以通过纯C代码调用这些函数。

(当然,你需要定义一个头文件。)

例如,如何转换下面的代码转换成纯c代码?

类PARENT
公共:
virtual int Init();
virtual int Add();
virtual int Sub();
int Otherfunc();

私有:
int x;
int y;
int z;
};

class CHILD1:public PARENT
{
public:
int Init();
int Add();
int CHILDOwn();
privete:
int a;
int b;
int c;
};

类CHILD2:public家长
{
公开:
int Init();
int Add();
int Uncle();
privete:
int m;
int n;
int d;
};

PARENT * CreateNewInstance() {
PARENT * pPointer = new CHILD1;
返回pPointer;
}

main()
{
PARENT * Kids1 = CreateNewInstance();
Kids1-> Uncle(); //来自儿童班
Kids1-> Sub(); //来自父类
Kids1-> Otherfunc(); //来自父类
}




无论如何你的C ++代码不是如此复杂,你可以将它们转换为C等价物。对于上面的代码,

1.用struct替换class

2.用函数指针替换virtual

3.将所有父类成员放入子类

4.为你使用的所有struct(类)编写初始化函数

并且它们应该返回该struct(class)的已分配对象。

,但它有些复杂,因为你应该处理

继承。

5.你可能需要编写析构函数来释放分配的所有内存

初始化函数。


实际上,如果你想在C中学习更多OO方法,我想你会

更好地阅读一些关于COM的书籍或文章。

Hanzac


文章< c3 ****** ********************@posting.google.com>,

bugzilla< mo ********* *****@gmail.com>写道:

:我有一个C ++程序需要转换为c语言才能在

:emabedded系统中使用。问题是原始代码是写的

:用C ++语言和Parent类以及一些子类。我怎么能通过在C语言中使用struct将这些C ++代码转换为纯c代码?


早期的C ++编译器是转换输入的预处理器

进入C代码。如果要转换的程序没有使用一些更现代的C ++构造,那么你可以使用这种自动翻译来使用

。 />
-

注意,有骆驼!





bugzilla写道:

全部,
我有一个C ++程序需要转换为c语言才能用于电子邮件系统。问题是原始代码是用C ++语言编写的,带有Parent类和一些子类。如何通过在C语言中使用struct将这些C ++代码转换为纯c代码?

有人可以给我任何想法吗?谢谢。

例如,如何将以下代码转换为纯c代码?

类PARENT
{
公开:
虚拟int Init();
virtual int Add();
virtual int Sub();
int Otherfunc();

private:
int x ;
int y;
int z;
};

class CHILD1:public PARENT
{
public:
int Init();
int Add();
int CHILDOwn();
privete:
int a;
int b;
int c; <课程CHILD2:公共父母
{
公开:
int Init();
int Add();
int Uncle();
privete:
int m;
int n;
int d;
};

PARENT * CreateNewInstance ()
{PARENT * pPointer = new CHILD1;
返回pPointer;
}

main()
{
父母*孩子们1 = CreateNewInstance();
Kids1-> Uncle(); //来自儿童班
Kids1-> Sub(); //来自父类
Kids1-> Otherfunc(); //来自父类
}




如何嵌入?如果没有C ++编译器,它通常意味着一个小的8位
CPU。如果案件重写它,我这个。有一个原因,许多8位cpus

没有C ++编译器。


hi,all,
I have a C++ program need to convert to c language to be used in a
emabedded system. the problem is that the original code was writtern
in C++ language with Parent class and some child class. How can I
invert these C++ code into pure c code by using struct in C language?

Can somebody give me any ideas? thanks.

For example, how to conver the following code into pure c code?

class PARENT
{
public:
virtual int Init();
virtual int Add();
virtual int Sub();
int Otherfunc();

private:
int x;
int y;
int z;
};
class CHILD1: public PARENT
{
public:
int Init();
int Add();
int CHILDOwn();
privete:
int a;
int b;
int c;
};

class CHILD2: public PARENT
{
public:
int Init();
int Add();
int Uncle();
privete:
int m;
int n;
int d;
};

PARENT* CreateNewInstance()
{
PARENT* pPointer=new CHILD1;
return pPointer;
}

main()
{
PARENT * Kids1=CreateNewInstance();
Kids1->Uncle(); //from children class
Kids1->Sub(); //from parent class
Kids1->Otherfunc();//from parent class
}

解决方案

Hi,

bugzilla wrote:

hi,all,
I have a C++ program need to convert to c language to be used in a
emabedded system. the problem is that the original code was writtern
in C++ language with Parent class and some child class. How can I
invert these C++ code into pure c code by using struct in C language?

Can somebody give me any ideas? thanks.
A better way is that you write a extern "C" wrapper functions around
the C++ program, and then compiled both the wrapper and the program
into a static library.

Then you will be able to call these functions through pure C code.
(Of course, you would need to define a header file.)
For example, how to conver the following code into pure c code?

class PARENT
{
public:
virtual int Init();
virtual int Add();
virtual int Sub();
int Otherfunc();

private:
int x;
int y;
int z;
};
class CHILD1: public PARENT
{
public:
int Init();
int Add();
int CHILDOwn();
privete:
int a;
int b;
int c;
};

class CHILD2: public PARENT
{
public:
int Init();
int Add();
int Uncle();
privete:
int m;
int n;
int d;
};

PARENT* CreateNewInstance()
{
PARENT* pPointer=new CHILD1;
return pPointer;
}

main()
{
PARENT * Kids1=CreateNewInstance();
Kids1->Uncle(); //from children class
Kids1->Sub(); //from parent class
Kids1->Otherfunc();//from parent class
}



Anyway if your C++ code is not so complex, you can convert
them into C equivalent. for the code above,
1. replace class with struct
2. replace virtual with function pointer
3. put all parent class members into the child class
4. write initializing functions for all the struct (class) you use
and them should return allocated object of that struct (class).
but it''s some complex because you should take care of the
inheritance.
5. You may need to write destructor to free all the memory allocated
by the initializing function.

Actually, if you want to learn more OO method in C, I think you''d
better read some books or articles about COM.
Hanzac


In article <c3**************************@posting.google.com >,
bugzilla <mo**************@gmail.com> wrote:
:I have a C++ program need to convert to c language to be used in a
:emabedded system. the problem is that the original code was writtern
:in C++ language with Parent class and some child class. How can I
:invert these C++ code into pure c code by using struct in C language?

The early C++ compilers were preprocessors that converted the input
into C code. If the program to be converted does not use some of the
more modern C++ constructs, then you might be able to make
use of that kind of automatic translation.
--
Look out, there are llamas!




bugzilla wrote:

hi,all,
I have a C++ program need to convert to c language to be used in a
emabedded system. the problem is that the original code was writtern
in C++ language with Parent class and some child class. How can I
invert these C++ code into pure c code by using struct in C language?

Can somebody give me any ideas? thanks.

For example, how to conver the following code into pure c code?

class PARENT
{
public:
virtual int Init();
virtual int Add();
virtual int Sub();
int Otherfunc();

private:
int x;
int y;
int z;
};

class CHILD1: public PARENT
{
public:
int Init();
int Add();
int CHILDOwn();
privete:
int a;
int b;
int c;
};

class CHILD2: public PARENT
{
public:
int Init();
int Add();
int Uncle();
privete:
int m;
int n;
int d;
};

PARENT* CreateNewInstance()
{
PARENT* pPointer=new CHILD1;
return pPointer;
}

main()
{
PARENT * Kids1=CreateNewInstance();
Kids1->Uncle(); //from children class
Kids1->Sub(); //from parent class
Kids1->Otherfunc();//from parent class
}



How embedded? If there is no C++ compiler it usually means a small 8 bit
CPU. I this if the case rewrite it. there is a reason many 8 bit cpus
do not have C++ compilers.


这篇关于如何使用C结构在C中实现C ++类遗产属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆