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.
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: -
class Foo
-
{
-
protected: int bar;
-
public: Foo( );
-
public: void doSomething();
-
protected: void doSomeOtherThing();
-
};
-
Could translate to: -
#ifndef FOOCLASS_H
-
#define FOOCLASS_H 1
-
typedef struct
-
{
-
int bar;
-
} Foo;
-
Foo * Foo_( Foo *this ); /* Constructor */
-
void Foo_doSomething( Foo *this ); /* Method translated into function*/
-
#endif
-
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.
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.
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).
thanks guys for your answers. I will discuss your suggestions with my teammates, and give you some updates soon.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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....
|
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...
|
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,...
|
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...
|
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...
|
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
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
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
|
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...
| |