Code:#include<stdio.h> struct class { int a; float b; void (*c)(); }o; void print() { printf("%d%d",1,2); } int main() { o.c=&print; o.a=5; o.b=18; printf("%d%d",o.a,o.b); (*o.c)(); return 0; }
Code:#include<stdio.h> struct class { int a; float b; void (*c)(); }o; void print() { printf("%d%d",1,2); } int main() { o.c=&print; o.a=5; o.b=18; printf("%d%d",o.a,o.b); (*o.c)(); return 0; }
This is how the code should look like.Code:#include<stdio.h> struct class { int a; float b; void (*c)(); } o; void print() { printf("%d%d",1,2); } int main() { o.c=&print; o.a=5; o.b=18; printf("%d%d",o.a,o.b); (*o.c)(); return 0; }
You aren't defining classes here at all. You're just making a function pointer and will cause the program to crash.
Was that your question?
Why will it cause the program to crash? It works ok for me but I had to change %d to %f in the first printf.You're just making a function pointer and will cause the program to crash.
Code:#include<stdio.h> struct class { int a; float b; void (*c)(); } o; void print() { printf("%d %d\n",1,2); } int main() { o.c=&print; o.a=5; o.b=18; printf("%d %f\n",o.a,o.b); (*o.c)(); return 0; }
Wait, I see you actually assigned that function pointer, but it's a really poor name, which might be why I missed it.
Yes, it will work.
You just found a way to "emulate" classes in C.
Still, no public/protected/private/virtual or inheritance, so it's pretty limited.
Was just thinking how messy the code would be if we start thing of implementing these scoped in C such as public/private/protected. All those member which lies under a specfic scope should be warapped in some way to maintaine the scope of public/private and protected.
ssharish
constructors and destructors are implemented as regular functions that should be called once by the programmer manually
inheritance is implemented as structures members with casting
pointer to struct derived points to the first element so it can be casted from one to anotherCode:struct base { int a; }; struct derived { struct base b; int c; };
virtual functions are implemented as function pointers members - as shown above - and initialized by the constructor functions
scope if implemented by returning a handle to the object and not an object itself
"public" functions are placed in the header that it available to the user of the object. each such funtion receives the handle - casts it inside to the actual object pointer and works with it
the object structure is not know to the user of the object
all "private" functions are implemented as static or put into separate header that is not available to the user of the object. These functions receive the pointer to object itself
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
@ all, i mistakenly posted u the unedited version!
void main() == void brain(). ma mistake, was just saving a line of coding there.
"%f" missed, again de to unedited piece, very sorry. am new to ur place, so forgive me, ma first time!
i know that it does'nt fully implement all the properties of classes, but that's what i came here for, to upgrade this to a fully working way of modelling classes through c!
hope u now know what i intented!
Worth a read perhaps?
http://ldeniau.home.cern.ch/ldeniau/html/oopc/oopc.html
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
If you want classes, why can't you use C++ instead of jumping through all these hoops to get a poor imitation of classes?
I believe it's just an intellectual exercise to try to mimic the classes using just C.
Or you could just re-implement C-Front from scratch, which is how the original C++ compiler worked.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
What is the purpose of this work?