How to implement C++ class heritage properties in C using C struct?

B

bugzilla

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
}
 
H

hanzac

Hi,
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
 
W

Walter Roberson

: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.
 
N

Neil Kurzman

bugzilla said:
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.
 
T

Tor Rustad

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);
}
 
B

bugzilla

: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.
 
R

Roger Leigh

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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?

http://www.le-hacker.org/papers/gobject/
http://developer.gnome.org/arch/gtk/object.html
http://developer.gnome.org/doc/API/2.0/gobject/index.html
http://www.lore.ua.ac.be/Teaching/Thesis2LIC/2003-2004/Hendrickx.pdf

(Don't worry about any GNOME bias--GType/GObject is all standard C
with no dependencies other than GLib and libc.)


Regards,
Roger

- --
Roger Leigh
Printing on GNU/Linux? http://gimp-print.sourceforge.net/
Debian GNU/Linux http://www.debian.org/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>

iD8DBQFCO/8SVcFcaSW/uEgRAtUhAKDI44R0egs+2ZdcMmSrOaQ/D4wvAQCfad0H
LSBjlKJ4sSZbU5KqGnCJH2w=
=P/4V
-----END PGP SIGNATURE-----
 
J

Jack Klein

Hi,


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.
 
W

Walter Roberson

:[email protected] (Walter Roberson) wrote in message :> 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'.
It is possible that the source is available somewhere. Some random links:
http://www.accu.org/acornsig/public/cathlib/docs/probs.html
http://compilers.iecc.com/comparch/article/94-11-070 (watch the date!)
http://homepage.ntlworld.com/thouky/software/cathlibcpp/docs/casts.html

See also 'Cback',
http://www.faqs.org/faqs/C++-faq/libraries/part4/



There were others, many essentially derivatives of cfront:

HP's C++ with the -F option, http://docs.hp.com/en/92501-90029/ch03s02.html
and SGI's C++ Translator http://techpubs.sgi.com
http://www.risc.uni-linz.ac.at/education/courses/ws2003/intropar/origin-new/CC_PG/sgi_html/apb.html
Sun had one as well.

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/software/kccdoc/v4.0/doc/migrate/
 
H

Hanzac Chen

Hi, Jack

Jack said:
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?
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
 
G

Greg Comeau

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.
 
G

Greg Comeau

: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.
 
G

Greg Comeau

Why do you want to do this? Because there is no C++ compiler available?
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.
 
G

Greg Comeau

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?


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.
 
G

Greg Comeau

:[email protected] (Walter Roberson) wrote in message :> 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/software/kccdoc/v4.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.
 
G

Greg Comeau

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top