Thread: implementin classes through c

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    3

    implementin classes through c

    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;
     }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    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?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    You're just making a function pointer and will cause the program to crash.
    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.
    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;
    }

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    And your question was ... ?

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    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.
    More importantly, no constructors or destructors, which means you have to make sure the "objects" are constructed & destructed manually.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    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

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    Code:
    struct base
    {
       int a;
    };
    
    struct derived
    {
       struct base b;
       int c;
    };
    pointer to struct derived points to the first element so it can be casted from one to another

    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

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    @ 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!

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vart View Post
    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
    Code:
    struct base
    {
       int a;
    };
    
    struct derived
    {
       struct base b;
       int c;
    };
    Many compilers also allow "anonymous" struct members, meaning you could write:
    Code:
    struct derived
    { 
        struct base;
        int c;
    };
    --
    Mats
    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.

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you want classes, why can't you use C++ instead of jumping through all these hoops to get a poor imitation of classes?

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I believe it's just an intellectual exercise to try to mimic the classes using just C.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

  15. #15
    Registered User
    Join Date
    Jan 2008
    Posts
    18
    What is the purpose of this work?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM