473,320 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

conversion from C++ to C questions

2
Hi, Im a new member here. I found this site by googling some C concepts.

I have written a code in C++ but I need to convert it to C so it can be used on a robot microprocessor. The code is well written in C++, which means it uses the OO features that C++ has. In my conversion to C I would like to keep some of the structure I had there, so that the converted code is easily read and easily expanded.

I have many concerns, so I will try to list them one at a time, adding more if I get feedback from you guys.

First one:
To transfer classes from C++ to C, I am thinking of using of converting each class to a "typedef struct" and store it in a .h file, that way I can keep things clean.
Is it the best way to do it?

Thanks,
GSA

P.S: I will post more questions as the conversation goes on.
Oct 27 '07 #1
4 5020
Yes, you can do that. Besides declaring the structure, you will need to declare the public methods of the class in the header file; the C equivalent of a C++ method is a function whose first parameter is a pointer to the structure (AKA the this pointer). Oh and dont forget the constructors too.

You know something like:

Expand|Select|Wrap|Line Numbers
  1. class Foo
  2. {
  3. protected: int bar;
  4. public: Foo( );
  5. public: void doSomething();
  6. protected: void doSomeOtherThing();
  7. };
  8.  
Could translate to:
Expand|Select|Wrap|Line Numbers
  1. #ifndef FOOCLASS_H 
  2. #define  FOOCLASS_H 1
  3. typedef struct
  4. {
  5. int bar;
  6. } Foo;
  7. Foo * Foo_(  Foo *this ); /* Constructor */
  8. void Foo_doSomething( Foo *this ); /* Method translated into function*/
  9. #endif
  10.  

But that is just the begining, there are lots of other details, and the code becomes more difficult to translate as you start using polymorphism.
Oct 28 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
The robot microprocessor only has a C compiler??

If so, that C compiler uses libraries like the ones provided to support C. That being the case, compile all of your C++ code in to a library and use the library in the C program that drives the robot. You call your C++ functions directly from C. All you may need to do is write a C API in C++ that calls the C++ funcitons that are already written.

You should be able to work around a conversion from C++ to C, which you will find is a ton of work and usually a waste of time.
Oct 28 '07 #3
Banfa
9,065 Expert Mod 8TB
If so, that C compiler uses libraries like the ones provided to support C. That being the case, compile all of your C++ code in to a library and use the library in the C program that drives the robot.
I assume you've missed a ++ of the second C.

However this solution still pre-supposes that a C++ compiler exists for the processor which the OP has said already does not exist as far as they know. That is how would you compile the C++ source into a library to be linked with the C program (I am aware that this is trivially easy for a platform that has C and C++ compilers (normally combined into 1 compiler)).


gsa it may be worth letting us know what the actual target is (i.e. the processor, because we may know of other solutions).
Oct 28 '07 #4
gsa
2
thanks guys for your answers. I will discuss your suggestions with my teammates, and give you some updates soon.
Nov 6 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: john | last post by:
Here is the short story of what i'm trying to do. I have a 4 sided case labeling printer setting out on one of our production lines. Now then i have a vb.net application that sends data to this...
9
by: Ben Midgley | last post by:
Please have a look at the code below My problem is the error message "Nonportable pointer conversion", so far as I can tell there is no pointer conversion occuring here so I am assuming it is...
2
by: Alex Sedow | last post by:
Why explicit conversion from SomeType* to IntPtr is not ambiguous (according to standart)? Example: // System.IntPtr class IntPtr { public static explicit System.IntPtr (int); public...
8
by: Duncan Winn | last post by:
I am new to VC++7. I am using a method GetPrivateProfileString that requires an LPTSTR. I have defined this as a: char * data_name; I am then trying to convert this to an LPOLESTR and I...
9
by: Vince | last post by:
Hi all, I am trying to convert a VB 6 application to VB.NET 2005 using the wizard. It has over 20 forms and I've used Option Explicit everywhere, hence variables are properly declared and used....
1
by: hunter hou | last post by:
Hello,Please look at the following code(from C++ in a nutshell) and my questions.Thanks,***Hunter... typedef void (*strproc)(const char*); void print(const char* str) { std::cout << "const...
11
by: santosh | last post by:
Hello all, Conversion macros along the name of INT8_C, INT16_C etc, are defined in stdint.h to convert their argument into suitable representations for their corresponding types, i.e. int8_t,...
29
by: aarthi28 | last post by:
Hi, I have written this code, and at the end, I am trying to write a vector of strings into a text file. However, my program is nor compiling, and it gives me the following error when I try to...
5
by: massimoal | last post by:
Hi all, I need to convert a tif image, grabbed by a pixelink industrial camera, to a text file (1024x768 elements) but I really don't know how. Surfing the net I found that there are a lot of...
7
by: PawelCarqowski | last post by:
Hallo group members, Do You know any conversion specification for this. I imagine: struct timeval time; printf("%T\n", time); regards, Pawel
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.