 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
bugzilla Guest
|
Posted: Sat Mar 19, 2005 10:39 am Post subject: How to implement C++ class heritage properties in C using C |
|
|
hi,all,
I have a C++ program need to convert to c language to be used in a
emabedded system. the problem is that the original code was writtern
in C++ language with Parent class and some child class. How can I
invert these C++ code into pure c code by using struct in C language?
Can somebody give me any ideas? thanks.
For example, how to conver the following code into pure c code?
class PARENT
{
public:
virtual int Init();
virtual int Add();
virtual int Sub();
int Otherfunc();
private:
int x;
int y;
int z;
};
class CHILD1: public PARENT
{
public:
int Init();
int Add();
int CHILDOwn();
privete:
int a;
int b;
int c;
};
class CHILD2: public PARENT
{
public:
int Init();
int Add();
int Uncle();
privete:
int m;
int n;
int d;
};
PARENT* CreateNewInstance()
{
PARENT* pPointer=new CHILD1;
return pPointer;
}
main()
{
PARENT * Kids1=CreateNewInstance();
Kids1->Uncle(); //from children class
Kids1->Sub(); //from parent class
Kids1->Otherfunc();//from parent class
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maxim Yegorushkin Guest
|
Posted: Sun Mar 20, 2005 1:54 am Post subject: Re: How to implement C++ class heritage properties in C usin |
|
|
bugzilla wrote:
| Quote: | hi,all,
I have a C++ program need to convert to c language to be used in a
emabedded system. the problem is that the original code was writtern
in C++ language with Parent class and some child class. How can I
invert these C++ code into pure c code by using struct in C language?
Can somebody give me any ideas? thanks.
|
Something along these lines:
/* parent.h */
/* parent interface */
struct parent;
/* virtual functions */
void parent_foo(parent*);
void parent_bar(parent*);
/* virtual ctors */
parent* create_parent();
parent* create_child();
/* parent.c */
#include <stdio.h>
#include <malloc.h>
struct parent
{
/* vtable */
void(*foo)(parent* self);
void(*bar)(parent* self);
int state;
};
struct child
{
parent base;
int additional_state;
};
/* vtable thunks */
void parent_foo(parent* self) { self->foo(self); }
void parent_bar(parent* self) { self->bar(self); }
/* virtual functions impl */
void parent_foo_impl(parent* self) { printf("parent_foo_impl, state %dn",
self->state); }
void parent_bar_impl(parent* self) { printf("parent_bar_impl, state %dn",
self->state); }
void child_bar_impl(parent* self) { printf("child_bar_impl, additional
state %dn", ((child*)self)->additional_state); }
void parent_ctor(parent* self)
{
/* vtable init */
self->foo = parent_foo_impl;
self->bar = parent_bar_impl;
/* constructor */
self->state = 1;
}
void child_ctor(child* self)
{
/* base class ctor call */
parent_ctor((parent*)self);
/* vtable init, overrides bar */
self->base.bar = child_bar_impl;
/* constructor */
self->base.state = 2;
self->additional_state = 3;
}
parent* create_parent()
{
parent* self = (parent*)malloc(sizeof(parent));
parent_ctor(self);
return self;
}
parent* create_child()
{
child* self = (child*)malloc(sizeof(child));
child_ctor(self);
return (parent*)self;
}
/* main.c */
int main()
{
parent* p1 = create_parent();
parent* p2 = create_child();
parent_foo(p1);
parent_bar(p1);
parent_foo(p2);
parent_bar(p2);
}
--
Maxim Yegorushkin
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Srijit Kumar Bhadra Guest
|
Posted: Sun Mar 20, 2005 2:01 am Post subject: Re: How to implement C++ class heritage properties in C usin |
|
|
Hello,
Have you visited the site - Object Oriented Programming in C
([url]http://ldeniau.home.cern.ch/ldeniau/html/oopc/oopc.html)?[/url]
Best regards,
/Srijit
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Boat Guest
|
Posted: Sun Mar 20, 2005 9:00 am Post subject: Re: How to implement C++ class heritage properties in C usin |
|
|
"bugzilla" <mozilla.bugzilla (AT) gmail (DOT) com> wrote
| Quote: | hi,all,
I have a C++ program need to convert to c language to be used in a
emabedded system. the problem is that the original code was writtern
in C++ language with Parent class and some child class. How can I
invert these C++ code into pure c code by using struct in C language?
Can somebody give me any ideas? thanks.
For example, how to conver the following code into pure c code?
class PARENT
{
public:
virtual int Init();
virtual int Add();
virtual int Sub();
int Otherfunc();
private:
int x;
int y;
int z;
};
class CHILD1: public PARENT
{
public:
int Init();
int Add();
int CHILDOwn();
privete:
int a;
int b;
int c;
};
class CHILD2: public PARENT
{
public:
int Init();
int Add();
int Uncle();
privete:
int m;
int n;
int d;
};
PARENT* CreateNewInstance()
{
PARENT* pPointer=new CHILD1;
return pPointer;
}
main()
{
PARENT * Kids1=CreateNewInstance();
Kids1->Uncle(); //from children class
Kids1->Sub(); //from parent class
Kids1->Otherfunc();//from parent class
}
|
You likely won't want to do this as a straight transliteration to C. A C++
compiler does a lot of the garbage bookkeeping for you under the covers.
But, because I have a little time on my hands:
C++ objects and methods differ from C by introducing hidden (or implicit)
members and arguments. The "magic" of polymorphism is handled behind the
scenes by the compiler using those hidden members.
Polymorphic classes add a virtual table, a static jump table or array of
pointers to functions, one per class. Each instance of classes in that
family have a hidden, implicit pointer to the corresponding static jump
table. Each call to a virtual function is vectored using the corrresponding
jump table: PARENT_vtable, CHILD1_vtable, and CHILD2_vtable.
Non-static member functions have an implicit argument pointing to its data,
the 'this' pointer.
struct PARENT_vtable {
int (*Init)(void * this);
int (*Add)(void * this);
int (*Sub)(void * this);
};
int parent_doInit(void * this);
int parent_doAdd(void * this);
int parent_doSub(void * this);
int parent_doOtherfunc(void *this);
struct PARENT_vtable PARENT_vtbl[] = {
{ &parent_doInit, &parent_doAdd, &parent_doSub}
};
....
struct PARENT_vtable CHILD1_vtbl[] = {
{ &child1_doInit, &child1_doAdd, &parent_doSub }
}
....
struct PARENT_data
{
int x;
int y;
int z;
};
struct CHILD1_data {
int a;
int b;
int c;
};
struct PARENT
{
PARENT_vtable * vptr;
struct PARENT_data parent;
};
struct CHILD1 {
PARENT_vtable * vptr;
struct PARENT_data parent;
struct CHILD1_data child1;
};
struct PARENT * CreateNewInstance()
{
struct PARENT * this = (struct PARENT*)malloc(sizeof(CHILD1));
this->vptr = CHILD1_vtbl;
return this;
}
int vcall_Parent_Sub(struct PARENT* this)
{ return (this->vptr.*Sub)(this);
}
....
main()
{
struct PARENT * Kids1=CreateNewInstance();
// Kids1->Uncle(); //from children class
// An out and out error in C++.
// Can't call a CHILD2 method on a Child1.
// Kids1->Sub(); //from parent class
vcall_Parent_Sub(Kids1);
// Kids1->Otherfunc();//from parent class
parent_doOtherfunc(Kids1);
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Bronek Kozicki Guest
|
Posted: Sun Mar 20, 2005 11:47 pm Post subject: Re: How to implement C++ class heritage properties in C usin |
|
|
bugzilla wrote:
| Quote: | hi,all,
I have a C++ program need to convert to c language to be used in a
emabedded system. the problem is that the original code was writtern
|
I would seriously consider obtaining C++ compiler for your environment.
While they sometimes do not provide some useful features of C++, they
still allow you to do proper OOP. Example is
http://www.metrowerks.com/MW/Develop/Embedded/ . It is possible to do
OOP in C, as others demonstrated in this thread, however I would not use
it, unless there is really no choice. Emulating OOP in C will make your
code very complicated and less efficient than C++, or you would need to
optimize all high-level constructs by hand (in C++ compiler does it
automatically)
B.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ivan Vecerina Guest
|
Posted: Mon Mar 21, 2005 4:18 pm Post subject: Re: How to implement C++ class heritage properties in C usin |
|
|
"Bronek Kozicki" <brok (AT) rubikon (DOT) pl> wrote
| Quote: | bugzilla wrote:
hi,all,
I have a C++ program need to convert to c language to be used in a
emabedded system. the problem is that the original code was writtern
I would seriously consider obtaining C++ compiler for your environment.
|
I agree this option definitely should be considered.
GCC or Comeau (http://www.comeaucomputing.com/) also support a number
of embedded platforms.
| Quote: | It is possible to do
OOP in C, as others demonstrated in this thread, however I would not use
it, unless there is really no choice. Emulating OOP in C will make your
code very complicated and less efficient than C++, or you would need to
optimize all high-level constructs by hand (in C++ compiler does it
automatically)
|
You can do OOP in C, but for practical reasons, most likely you would
not implement it the same way. For instance, in small projects, it is
might be easier to have an enum field determining the type of an object
than do use a function table (but I have been using both approaches).
If you only have few "virtual" function pointers, it may also make sense
to store them within the instance itself, rather than in a global table.
Also, in C, it is usually better to use opaque handles, than to expose
a struct and its fields.
Regards,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
lulu yao Guest
|
Posted: Mon Mar 21, 2005 9:57 pm Post subject: Re: How to implement C++ class heritage properties in C usin |
|
|
I once wrote some codes to let me use simple c++ tricks such as single
inheritance and virtual functions. I learned this idea from Axel-Tobias
Schreiner in Object-oriented Programming in ANSI-C, I also defined
some macros for convenience
/*>>>>>>>>>>>>>>>>>>>> macro and type definitions
<<<<<<<<<<<<<<<<<<<<*/
// Type definition for class name.
#define class const rtti_t *
// Convert class x to a type name.
#define typename(x) struct _class_##x *
// Macros for class definition, used in header file only.
// DEFINE_CLASS(x) - to define class x from scratch.
// SUB_CLASS(x, Y) - to derive class x from y.
// MEMBER(m, t) - to define data member.
// END_CLASS(x) - to end class definition.
#define DEFINE_CLASS(x) struct _class_##x {
struct _vtbl_##x * vptr_;
#define SUB_CLASS(x, y) struct _class_##x {
union {
struct _vtbl_##x * vptr_;
struct _class_##y _;
};
#define MEMBER(m, t) t m;
#define END_CLASS(x) }; extern const rtti_t * x;
// Macros for virtual table definition, used in head file only.
// DEFINE_VTBL(x) - to define a vtbl for class x.
// VIRTUAL(f, r) - to insert an entry to vtbl.
// END_VTBL(x) - to end a vtbl definition.
#define DEFINE_VTBL(x) struct _vtbl_##x {
rtti_t * class_;
size_t size_;
class_ctor_t ctor_;
class_dtor_t dtor_;
class_clone_t clone_;
#define VIRTUAL(f, r) r (* f)();
#define END_VTBL(x) };
// Macros for virtual table generation, used in source file only.
// STUB_VTBL(x) - to generate vtbl for class x
// STUB(f) - to insert an entry to vtbl
// END_STUB(x) - to end a vtbl generation
#define STUB_VTBL(x) struct _vtbl_##x _rtti_##x = {
(rtti_t *)&_rtti_##x,
sizeof(struct _class_##x)
#define STUB(f) , f
#define END_STUB(x) }; const rtti_t * x = (rtti_t *)&_rtti_##x;
.....
I implemented some data structures using them, but I left it aside
quite a long time ago. If you still want to have a try, I suggest you
to searh the book I mentioned above
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
B.C. Guest
|
Posted: Mon Mar 21, 2005 10:08 pm Post subject: Re: How to implement C++ class heritage properties in C usin |
|
|
Use a front end c++ compiler such as the comeau one:
http://www.comeaucomputing.com/. Then you don't have to worry about
rewriting anything, the compiler will do it for you.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|