Quote:> Does anyone have an Idea as to how to implement object oriented design
> in language C ?
> Although this may lead to a little loss of performance in terms of
> coding and execution but I have seen many of the c++ compilers
generate
> the c code from c++ code and then compile it.
> Is there any standard for this. If yes what is it ?
I did this extensively in a situation where we had only a C compiler
and tools for an embedded DSP project. I was programming analysis
tools in Java at the time, and was able to fairly well do certain
aspects of Java-'inspired' OO using a set of macros that mechanized
single linear inheritance. The code looked something like:
class(FunctionGen)
{
extends(AbstractFunctionGen);
chain_delete(); /* 'virtual' destructor */
float (*getNextVal)(AbstractFunctionGen*); /* member function */
float someX;
float someY;
Quote:};
static float getNextVal(AbstractFunctionGen*pSuper)
{
LI_THIS(); /* extracts hidden pointer to this */
super.val = this.someX*this.someY;
this.someX *= 2.0;
Quote:}
CTOR(FunctionGen)
{
SUPER_CTOR(AbstractFunctionGen);
override(AbstractFunctionGen,getNextVal);
this.someX = 0.0;
this.someY = 0.0;
Quote:}
FuctionGen* a = new(FunctionGen); /* automatically invoked CTOR! */
and so on. All with naming tricks and macros. The key is exploiting
the fact that a pointer to a struct is also a pointer to the first
element of the struct to mechanize the principle that a pointer to a
derived class can be used anywhere a pointer to a base class is
expected (the LSP). There was a lot more to it than the simple example
above.
I'm sure others have done similar things. I just wanted to be clear on
my experience when I say:
DON'T DO THIS!! The only advantage is if you absolutely, positively
cannot get a C++ compiler for your target processor. The end product
using C/OO is slower, takes more dynamic memory, lot's more work, and
is far (far,far,far) less flexible than using C++. Note that doing OO
yourself in C isn't related to the fact that some C++ compilers output
C. The closer you try to match C++ (like, mechanizing VTables to avoid
a function pointer in each instance) the work just gets harder and
harder.
DON'T DO IT.
If you have a C++ compiler available, learn C++ and use that instead of
C (that's true beyond pure OO, C++ templates let you have your cake
(organized, layered design) and eat it too (throughput efficient)).
--
/\----|---------|
|| __o_________o_____
|| | Ye Olde Coder |
|| VVVVVVVVVVVVVVVVVV
Sent via Deja.com http://www.deja.com/
Before you buy.