473,763 Members | 7,727 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Port code from C++ to C?

Hi,

I have to use some machine with only support of C. But I have some C++
programs, which use most C++ comman features. Is there any way to
convert those C++ programs to C?

Or is there any way that I can mimic C++ by C? So that even if I have
to recode, I don't have to change the C++ code structures.

Thanks!

Best wishes,
Peng

Nov 14 '05 #1
13 4049
In article <11************ **********@g49g 2000cwa.googleg roups.com>,
Pe*******@gmail .com <Pe*******@gmai l.com> wrote:
I have to use some machine with only support of C. But I have some C++
programs, which use most C++ comman features. Is there any way to
convert those C++ programs to C?


Please see the thread starting from
http://groups.google.ca/group/comp.l...592bd669995492
--
"Who Leads?" / "The men who must... driven men, compelled men."
"Freak men."
"You're all freaks, sir. But you always have been freaks.
Life is a freak. That's its hope and glory." -- Alfred Bester, TSMD
Nov 14 '05 #2
Thank you for your reply! But this thread doesn't address all I want.

Not only I want port C++ to C (not necessarily by a preprocessor), I
also want the C code human readable.

So the problem right now is that how to implement OO in C language.
I've read some old OO book which discuss this issue, because there were
no enough OO support at that time. But I forget the title of the book.

If you can give me any relevant information, it will be great.

Peng

Nov 14 '05 #3
In article <11************ **********@g14g 2000cwa.googleg roups.com>,
Pe*******@gmail .com <Pe*******@gmai l.com> wrote:
Thank you for your reply! But this thread doesn't address all I want. Not only I want port C++ to C (not necessarily by a preprocessor), I
also want the C code human readable.
You indicated that you wanted to mimic C++ in C. The result is
not very likely to be human readable.

So the problem right now is that how to implement OO in C language.


Don't. If you need C++ then use C++. If you don't need C++ then
write in C paradigms, not in C++ paradigms. Any attempt to write a
hybrid is likely to end up with ugly code.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Nov 14 '05 #4


Pe*******@gmail .com wrote:
Thank you for your reply! But this thread doesn't address all I want.

Not only I want port C++ to C (not necessarily by a preprocessor), I
also want the C code human readable.

So the problem right now is that how to implement OO in C language.
I've read some old OO book which discuss this issue, because there were
no enough OO support at that time. But I forget the title of the book.

If you can give me any relevant information, it will be great.

Peng


You can do everything with C that you can do with C++ but if the code
is heavily dependent on OO features of C++ it is likely to be very
difficult to read if you simply translate it as is to C. You are
probably better of reverse engineering it and developing a design from
the existing C++ code and then adjusting the design to be implemented
in C using its strengths and then re-coding it.

Nov 14 '05 #5
"Pe*******@gmai l.com" <Pe*******@gmai l.com> writes:
Thank you for your reply! But this thread doesn't address all I want.

Not only I want port C++ to C (not necessarily by a preprocessor), I
also want the C code human readable.

So the problem right now is that how to implement OO in C language.
I've read some old OO book which discuss this issue, because there were
no enough OO support at that time. But I forget the title of the book.


Miro Samek discussed this extensively as part of his book "Practical
Statecharts in C/C++". He called the implementation "C+". You can
download the manual and source code from his web site
<http://www.quantum-leaps.com/devzone/cookbook.htm#OO P>.

--

John Devereux
Nov 14 '05 #6
I'm sorry that I cann't download this manual. It seems the above link
is dead. If you have a copy would please email it to me as attachment.
Thanks,

Peng

Nov 14 '05 #7
To do OO in C, you usually do something like this:

C++ Class
---------
class foo
{
int a;
virtual void bar() { a = 3; }
virtual void abc() { a = 4; }
};

class doo : public foo
{
int b;
void bar() { b = 5; } /* overrides the original bar */
virtual void xzy() { b = 6; }
};

The C Version:
--------------
struct foo;
struct vtable_foo
{
void (*bar)(struct foo*);
void (*abc)(struct foo*);
};
struct vtable_foo __vtable_foo;

struct foo
{
struct vtable_foo *vtbl;
int a;
};

void foo_bar(struct foo *f)
{
f->a = 3;
}

void foo_abc(struct foo *f)
{
f->a = 4;
}

/* class initializer -- must be run EXACTLY ONCE before creating any
instances of foo */
void initialize_foo_ class()
{
__vtable_foo.ba r = foo_bar;
__vtable_foo.ab c = foo_abc;
}

/* constructor */
void foo_initialize( struct foo *f)
{
f->vtbl = &__vtable_fo o;
f->a = 0;
}

struct doo;
struct vtable_doo
{
void (*bar)(struct doo*);
void (*abc)(struct doo*);
void (*xyz)(struct doo*);
};
struct vtable_doo __vtable_doo;

struct doo
{
struct vtable_doo *vtbl;
int a; /* from foo */
int b;
};

void doo_bar(struct doo *d)
{
d->b = 5;
}

void doo_xyz(struct doo *d)
{
d->b = 6;
}

void initialize_doo_ class()
{
/* inherited from foo */
__vtable_doo.ba r = doo_bar; /* overriden in doo */
__vtable_doo.ab c = foo_abc;

/* new in doo */
__vtable_doo.xy z = doo_xyz;
}

void doo_initialize( struct doo *d)
{
d->vtbl = &__vtable_do o;
d->a = 0;
d->b = 0;
}

int main()
{
struct foo *f1;
struct foo *f2;
struct doo *d1;

initialize_foo_ class();
initialize_doo_ class();

d1 = malloc(sizeof(s truct doo));
doo_initialize( d1);
f1 = malloc(sizeof(s truct foo));
foo_initialize( f1);
f2 = d1;
(d1->vtbl->xyz)(d1); /* this sets d1->b to 6 */
printf("d1->b is %d\n", d1->b);
(f2->vtbl->bar)(f2); /* this sets d1->b to 5, because it is invoke via
vtable */
printf("d1->b is %d\n", d1->b);
(f1->vtbl->bar)(f1); /* this sets f1->a to 3 */
printf("d1->a is %d\n", f1->a);

return 0;
}

----end program----

The problem w/ this approach is (a) its messy and error prone, and (b)
it only supports direct single-line inheritance. If you want to use
multiple inheritance or even mixin classes (abstract classes like java
interfaces), it gets much trickier, and (c) you have to do a LOT of writing.

Also, while we're on the topic you might check out my article on
closures in C, which relates very closely to what we're doing here:

http://www-128.ibm.com/developerwork...-highfunc.html

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
Nov 14 '05 #8
In article <11************ **********@g49g 2000cwa.googleg roups.com>,
Pe*******@gmail .com <Pe*******@gmai l.com> writes
Hi,

I have to use some machine with only support of C. But I have some C++
programs, which use most C++ comman features. Is there any way to
convert those C++ programs to C?

Or is there any way that I can mimic C++ by C? So that even if I have
to recode, I don't have to change the C++ code structures.

Thanks!

Best wishes,
Peng


I Think this solves your problem: Comeau C++ compiler generates C as its
object code.

Comeau C/C++ 4.3.3: Watch for our Mac port
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?
co****@comeauco mputing.com http://www.comeaucomputing.com

--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys. org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Nov 14 '05 #9
"Pe*******@gmai l.com" <Pe*******@gmai l.com> writes:
I'm sorry that I cann't download this manual. It seems the above link
is dead. If you have a copy would please email it to me as attachment.


My mail to you bounced with "illegal attachment" so I guess you are
stuck! I downloaded the file myself earlier today, but now the link
seems to be broken as you say. Perhaps it will work again soon!

--

John Devereux
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5055
by: collinm | last post by:
hi i send a command to a led display, the led display is suppose to return me some character i write a string on a serial port void ledDisplayExist() { char msg={'\0', '\0', '\0', '\0', '\0', '\1', 'Z', '0', '0',
13
4833
by: Al the programmer | last post by:
I need to access the serial ports on my webserver from an asp.net page. I have no problem accessing the serial ports from a windows form application, but the code doesn't work in asp.net. I have been told it is not possible to access the serial ports from asp.net. The application is used to control custom hardware. The hardware is connected to a PC through serial ports. Our customer wants to control the hardware from a remote...
5
18200
by: Darrell Wesley | last post by:
Is there a good source of information on how to read a USB port, in particular reading data from a BarCode reader?
4
11207
by: joe bloggs | last post by:
I am writing a mobile application to interface with a legacy system and I am planning to use web services to communicate with this system. The legacy system receives data through a serial port. What I would like to do is make the serial port accessible via a web service. The web service and the legacy application would be running on the same machine. The mobile application would access the web service via a network connection. It...
4
17816
by: Frank | last post by:
Hello, how to get information about all serial ports in the PC? I use the following code, but i got only the data of the FIRST serial port. All other serial port information are not available with this code sample: ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * from WIN32_SerialPort");
4
5069
by: H J van Rooyen | last post by:
Hi All, I am writing a polling controller for an RS-485 line that has several addressable devices connected. It is a small access control system. All is well- the code runs for anything from three hours to three days, then sometimes when I get a comms error and have to send out a nak character, it fails hard... The traceback below pops up. - the first lines are just some debug prints.
6
2818
by: swartzbill2000 | last post by:
If I do this: Dim receiver As New UdpClient() Then the documentation says the system will pick a port. How can I extract the chosen port number from receiver? Bill
0
5543
by: 14Dallas | last post by:
Hi, I have been working with another programmer to write this code. I haven't written code in years - DBase III and Pascal - anyways, back to my code question. The program opens a file and reads the contents that are seperated by spaces, not tabs - will never be seperated by tabs as it is output from another program. The contents of the file are essiantlly 3 fields/values # # Text Field 1 will be a number from 1 to 26, Field 2 will...
2
3792
by: evle | last post by:
haw to read data from an Infrared Infrared Remote Control
13
6209
by: Rob | last post by:
Hi all, I am fairly new to python, but not programming and embedded. I am having an issue which I believe is related to the hardware, triggered by the software read I am doing in pySerial. I am sending a short message to a group of embedded boxes daisy chained via the serial port. When I send a 'global' message, all the connected units should reply with their Id and Ack in this format '0 Ack' To be certain that I didn't miss a...
0
9386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9997
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9937
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7366
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5270
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
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 we have to send another system
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.