Board index » cppbuilder » General question about C and C++

General question about C and C++


2008-04-05 04:24:40 AM
cppbuilder1
I have a large base of code (2.5 million lines) currently written in some
other languages. Recently I started thinking that it might be useful to
have a few chunks of that code written in C or C++. It would also improve
my skills by translating some of it over to a new language. My only
experience with C or C++ is just a few brief (and I do mean brief) times and
that was quite a while ago. I'm guessing maybe a total of slightly less
than 1,000 lines total that I've ever done and as I said it was quite some
time ago now.
Since I've been out of it for a long time I just wondering about some
advice. I'm using BDS 2006 for the Pascal code I'm using and so I see that
I have C++ Builder available to use in that same install. I'm considering
the following issues:
1) The code has no interface issues to deal with. No GUI at all this is
just good old callable routines that do stuff. You know the type, a couple
of string params and a couple of integer params and it cranks and maybe
creates some file or returns some string or value. None of this initially
would be object oriented at all.
2) I want this to end up as a dll so I can call this from any of the other
languages that I'm working with (Delphi Pascal, Ada, Modula-2). I want ease
of use as far as connecting to this dll and passing parameters so no weird
param types. Just the usual pushing numbers or for strings pushing the
pointer and the size. I just mention this so you won't be thinking I'm
trying to pass objects of some type, this is just simple parameters.
3) I want maximum portability of the code so in case I want to use a
different compiler I can.
Now to the question. My main question is would I code this as C code or as
C++ code? I'm really out of the loop as far as knowing why I would use one
over the other for this basic type code? I guess I'm just wondering if the
C/C++ community out there has an opinion about why I would code it one way
or the other. I tried playing a little bit with getting a project started
and set up using C++ builder and it seemed that everything wanted to default
to C++ ways of doing stuff.
 
 

Re:General question about C and C++

"Warren O. Merrill" < XXXX@XXXXX.COM >writes:
Quote
2) I want this to end up as a dll so I can call this from any of the other
languages that I'm working with (Delphi Pascal, Ada, Modula-2). I want ease
of use as far as connecting to this dll and passing parameters so no weird
param types. Just the usual pushing numbers or for strings pushing the
pointer and the size. I just mention this so you won't be thinking I'm
trying to pass objects of some type, this is just simple parameters.
...
Quote
Now to the question. My main question is would I code this as C code or as
C++ code? I'm really out of the loop as far as knowing why I would use one
over the other for this basic type code? I guess I'm just wondering if the
C/C++ community out there has an opinion about why I would code it one way
or the other. I tried playing a little bit with getting a project started
and set up using C++ builder and it seemed that everything wanted to default
to C++ ways of doing stuff.
It's not too hard to write a C function that calls the C++ function,
so that you could write the real logic in C++, but make the interface
available to other language by exposing a C interface adapter.
If you skip C++ and write it entirely in C, then you won't have that
problem. Or mix the two. Only the outermost functions *need* to be
in C, however, and the internals can be in whatever you want. Just be
sure to keep memory management inside the DLL, and not to expect
callers to release any memory. If you allocate objects and pass them
out of the dll, you need to make an interface so that the clients can
pass the objects back into the dll and have IT destroy the object for
them. Otherwise you can encounter serious (and subtle) memory errors.
--
Chris (TeamB);
 

Re:General question about C and C++

"Warren O. Merrill" < XXXX@XXXXX.COM >wrote in message news:47f68e86$ XXXX@XXXXX.COM ...
Quote
Now to the question. My main question is would I code this as C code or as C++ code? I'm really out of the loop as far as
knowing why I would use one over the other for this basic type code? I guess I'm just wondering if the C/C++ community out there
has an opinion about why I would code it one way or the other.
You might look into struct vs. class. In C++ you abstract your data in classes. These classes or objects
may contain functions but they don't have to. You could just pass around your class to the various
functions that need it. When you get ready to pass your class to a DLL you will need to serialize
it. Consider the following example:
// Here we have our data. It is a C++ class. It has two functions to change and read properties.
class Person
{
private:
int m_Age;
std::string m_Name;
public:
Person();
~Person();
void SetAge(int iAge);
void SetName(std::string sName);
int GetAge(void);
std::string GetName(void);
};
//Some functions to show use
myFunctionThatNeedClass(Person* pPerson)
{
pPerson->SetAge(31);
pPerson->SetName("Joe");
}
myFunctionThatSerializesMyClass(Person* pPerson)
{
char * szAge = new char[pPerson->GetName().size() + 1];
int iAge = pPerson->GetAge();
//Call dll using char and int or whatever...
delete[]szAge;
}
// Of course what fun is it with one person?
std::vector<Person>myPersonList;
Person aPerson;
aPerson.SetAge(31);
apPerson.SetName("Joe");
myPersonList.push_back(apPerson);
for (size_t i = 0; i < myPersonList.size(); i++)
{
std::cout << myPersonList[i].GetName() << end1;
}
 

{smallsort}