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
} 15 3757
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. ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in message news:<d1**********@canopus.cc.umanitoba.ca>... 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.
Thanks. But where can I find such compiler? can you give me some
useful link? thanks.
"bugzilla" <mo**************@gmail.com> wrote in message
<snip> 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; };
Alternative 1:
-----------------
struct PARENT
{
int x, y, z;
};
int Init(struct PARENT *);
int Add(struct PARENT *);
int Sub(struct PARENT *);
int Otherfunc(struct PARENT *);
Alternative 2:
------------------
struct PARENT
{
int x, y, z;
int (*Init) (struct PARENT *);
int (*Add) (struct PARENT *);
int (*Sub) (struct PARENT *);
int (*Otherfunc) (struct PARENT *);
};
using function pointers, add more labour, since
you need to initialilze each function pointer e.g.
struct PARENT p;
p.Init = Init();
p.Add = Add();
etc.
class CHILD1: public PARENT { public: int Init(); int Add(); int CHILDOwn(); privete: int a; int b; int c; };
int CHILDOwn(struct PARENT *);
class CHILD2: public PARENT { public: int Init(); int Add(); int Uncle(); privete: int m; int n; int d; };
int Uncle(struct PARENT *);
PARENT* CreateNewInstance() { PARENT* pPointer=new CHILD1; return pPointer; }
struct PARENT* New_Parent()
{
struct PARENT *p;
/* what if no memory??? */
p = malloc( sizeof *p);
if (p == NULL)
exit(EXIT_FAILURE);
/* init members */
p->x = p->y = p->z = 0;
/*if using function pointers, init now.. */
return p;
}
main() { PARENT * Kids1=CreateNewInstance(); Kids1->Uncle(); file://from children class Kids1->Sub(); file://from parent class Kids1->Otherfunc();//from parent class }
int main( void)
{
struct PARENT kid = {0};
Uncle (&kid);
Sub (&kid);
Otherfunc (&kid);
}
.... or if you want to use PARENT pointer:
int main( void)
{
struct PARENT *pkid = New_Parent();
Uncle (pkid);
Sub (pkid);
Otherfunc (pkid);
}
--
Tor <torust AT online DOT no>
On 18 Mar 2005 15:55:29 -0800, ha****@gmail.com wrote in comp.lang.c: 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.
Where in the C standard does it define "extern "C" wrapper functions"
or static libraries? Aside from being way off-topic, do you even
understand the limitations of the OP's situation?
Then you will be able to call these functions through pure C code. (Of course, you would need to define a header file.)
On an embedded platform that doesn't have a C++ compiler in the first
place? Regardless of platform, C neither defines, requires, nor
guarantees interoperability with C++, or any other language for that
matter.
[big snip of OP's off-topic C++ code]
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.
COM is completely off-topic here as well.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Hi, Jack
Jack Klein wrote: Where in the C standard does it define "extern "C" wrapper functions" or static libraries? Aside from being way off-topic, do you even understand the limitations of the OP's situation?
Are they related to the C standard or related to the code generator
and the linker? AS for the OP's situation, I know that if using OP
it will need a considerable backend runtime library. But if it's a
big CPP project, and you want to use it in a C project, will you still
want to convert its CPP code into C? Are there any automatic tools
available and they won't bring any trouble? Then you will be able to call these functions through pure C code. (Of course, you would need to define a header file.)
On an embedded platform that doesn't have a C++ compiler in the first place? Regardless of platform, C neither defines, requires, nor guarantees interoperability with C++, or any other language for that matter.
Doesn't GCC support most of the modern CPUs? I think it's quite good
for developing on an embedded platform.
Why do you just see things literally? actually C compiler is always
being implemented powerful enough to be able to do most of the tasks.
So however the C++ code is generated, as long as it's not virtual and
can run on a real machine, it should be able to be accessed by C code.
COM is completely off-topic here as well.
I think as COM (Component Object Model) is language free and C can also
access COM interfaces and implement them, there are articles and
techniques out there. OK, I see GObject is also mentioned, so it might
be a better choice.
I don't mean to argue with you, it's just IMHO, and I'm not quite
familiar with CPP 'cause I tend to use C and prefer C.
Hanzac
In article <11*********************@g14g2000cwa.googlegroups. com>,
<ha****@gmail.com> wrote: 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.
Assuming the OP wants to do this because s/he doesn't have
a C++ compiler, that clearly won't work.
Then you will be able to call these functions through pure C code. (Of course, you would need to define a header file.)
And probably need to deal with a whole bunch of other
non-obvious ways of doing things.
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.
Yes, things like this would need to be done.
But as with any hand conversion, is error prone, tedious,
and as you note, may depend upon whether the C++ code is
complex or not (for some definition of "complex").
This also does not deal with any room for dealing with
idioms, design, etc.
--
Greg Comeau / Comeau for the Mac? Stay tuned.
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
In article <d1**********@canopus.cc.umanitoba.ca>,
Walter Roberson <ro******@ibd.nrc-cnrc.gc.ca> wrote: 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.
Early versions of Comeau C++ were based upon cfront.
Early C++ compilers were no more preprocessors than any compiler is.
Early C++ _compilers_ were... well, compilers :)
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.
Actually, the most compliant C++ compiler and most modern C++ compiler
makes use of such automatic translations, and hence also allows
very modern C++ constructs. Modern versions of Comeau C++ are
still integrated to do this, though they no longer are cfront based.
--
Greg Comeau / Comeau for the Mac? Stay tuned.
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
In article <c3**************************@posting.google.com >,
bugzilla <mo**************@gmail.com> wrote: ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in message news:<d1**********@canopus.cc.umanitoba.ca>... 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?
Why do you want to do this? Because there is no C++ compiler available?
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.
Thanks. But where can I find such compiler? can you give me some useful link? thanks. http://www.comeaucomputing.com But do note that you are probably
seeking customizations in order to obtain a C++ compiler, and so
should probably email us.
--
Greg Comeau / Comeau for the Mac? Stay tuned.
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
In article <mk********************************@4ax.com>,
Jack Klein <ja*******@spamcop.net> wrote: On 18 Mar 2005 15:55:29 -0800, ha****@gmail.com wrote in comp.lang.c: bugzilla 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? > > 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.
Where in the C standard does it define "extern "C" wrapper functions" or static libraries? Aside from being way off-topic, do you even understand the limitations of the OP's situation?
Then you will be able to call these functions through pure C code. (Of course, you would need to define a header file.)
On an embedded platform that doesn't have a C++ compiler in the first place?
Good points.
Regardless of platform, C neither defines, requires, nor guarantees interoperability with C++, or any other language for that matter.
Though on a practical matter, it's often possible to have your
cake and eat it too. That is to say, the interoperatability can
often be achieved, though not as the poster is saying, and often
with limitations, and at a loss of things like portability.
But then, that's often a given and even desirable given the
nature of the beast.
--
Greg Comeau / Comeau for the Mac? Stay tuned.
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
In article <d1**********@canopus.cc.umanitoba.ca>,
Walter Roberson <ro******@ibd.nrc-cnrc.gc.ca> wrote: In article <c3**************************@posting.google.com >, bugzilla <mo**************@gmail.com> wrote: :ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in message news:<d1**********@canopus.cc.umanitoba.ca>... :> The early C++ compilers were preprocessors that converted the input :> into C code.
:Thanks. But where can I find such compiler? can you give me some :useful link? thanks.
"AT&T C++ TRANSLATOR" is the best known. This was known as 'cfront'. ...
But also limited, defunct and extremely outdated at this point.
I suspect KAI's C++ might be the obscure non-cfront that the iecc article refered to. I haven't heard of KAI (Kuck and Associates) for a long time... http://zampano.zam.kfa-juelich.de/so...0/doc/migrate/
Yes, KAI and it's compiler no longer exist as such.
As to "obscure non-cfront", I guess call it whatever you want
(Stroustrup has called it "son of cfront" FYI), but it still
fills needs. If it didn't, Comeau Computing wouldn't exist,
at least not in our current form.
--
Greg Comeau / Comeau for the Mac? Stay tuned.
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
In article <11**********************@f14g2000cwb.googlegroups .com>,
Hanzac Chen <ha****@gmail.com> wrote: Jack Klein wrote: Where in the C standard does it define "extern "C" wrapper functions" or static libraries? Aside from being way off-topic, do you even understand the limitations of the OP's situation? Are they related to the C standard or related to the code generator and the linker?
Not as such. The Standard talks about things which need to get done,
so it won't talk of static libraries themselves (or other kinds)
just that resolution of names should occur, and so on. And of course
it doesn't know anything about extern "C". Of course, both the
C++ and C standard are setup so that there is fighting changes
across compilers and even languages, but it can only go so far,
and there are no guarantees. So lots is left to vendors to get
going (or not) in specific implementations.
AS for the OP's situation, I know that if using OP it will need a considerable backend runtime library. But if it's a big CPP project, and you want to use it in a C project, will you still want to convert its CPP code into C?
There is no requirement that cross language interoperability needs
for C++ to be converted to C. As as Jack pointed out it's beyond
the realm of the standard(s). So it's left to what vendors,
users, etc can get going.
Are there any automatic tools available
Comeau C++, after purposeful customization since it needs to
deal with plaform specific issues, as would any such tool.
and they won't bring any trouble?
As with any compiler, not if done right, and done within the realm
of the culture of the platform. IOWs, what was just said, as the
standards don't offer any guarantees, etc, and also certain platforms
have their own limitations and sensibilities.
actually C compiler is always being implemented powerful enough to be able to do most of the tasks. So however the C++ code is generated, as long as it's not virtual and can run on a real machine, it should be able to be accessed by C code.
It certainly possible in many cases, but it's "never" as simple as
just compiling it and linking it. Very far from that.
--
Greg Comeau / Comeau for the Mac? Stay tuned.
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Thomas |
last post by:
What's the quickest way to write and read 10.000 integer values ( or
more ) to and from a file? Using struct somehow? The example in the
docs shows how to handle to or three arguments, but is the...
|
by: Victor Hannak |
last post by:
I have a class like this:
<LarryClass.h>
class LarryClass {
public:
struct MoeLinkedListStruct {
LarryClass *Larry;
float MoeValue;
MoeLinkedListStruct *NextMoe;
};
|
by: Dawid Mostert |
last post by:
Hi,
I'm trying to get a list of properties from a hierarchy of classes,
using reflection.
Using Type.GetProperties(), I get the list back in "reverse hierarchy
order" (derived class...
|
by: Panchal V |
last post by:
I want to access a variable length record in C, the format is as
follows :
+---+---+-----------+
| A | L | D A T A |
+---+---+-----------+
A - Some Data (1 BYTE)
L - Length the Data that...
|
by: Sunny |
last post by:
Hi,
I have an old problem which I couldn't solve so far. Now I have found a post
in that group that gave me an idea, but I can not fully understand it.
The problem is: I'm trying to use a...
| |
by: Pent |
last post by:
Hi All,
Why is this code valid? How can the static ctor be used? It doesn't act as
class ctor at all.
struct A
{
static A() { }
}
|
by: Henke |
last post by:
I get a unresolved external error when linking this code. Does anybody know
why?
..h-file
namespace TestDll
{
__gc class Class1
{
public:
Class1();
|
by: Tugrul HELVACI |
last post by:
Changing DisplayNames of my properties using PropertyGrid component, how ??
I'm using Delphi 2006 and I have a class defination like this:
TPerson = class
fPersonName : String;...
|
by: df |
last post by:
We all know we can keep value of server control between post-back,but
how can do this with web form class member properties?For example,I
want to decare a class member property as array,to store...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |