CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2007
    Posts
    90

    Inheritance with C

    Hello,

    it is possible to make some cind of inheritance with c using structs and unions?

    Best regards,
    BoSCHoW.

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Inheritance with C

    As far as I know the very first C++ compiler was actually a C pre-processor so it can be done but I can't say to what extent it is practical.

    If you think of a module (c-file) handling a specific struct (or rather a pointer to a struct) as a class then this class can be extended with a second module that reuses the functionality in the first module. This inheritance would not be as seamless as in C++ so to overcome this you probably want to declare a new struct for every "inheritance" you make (sooner or later the maintainance work will probably be a pain in the...). There will also be a need for a lot of static casting adding up to the maintainance.

    Just a thought, maybe a C++ pre-processor still can be found/bought? Have you searched for instance sourceforge?
    Last edited by S_M_A; April 7th, 2008 at 03:05 AM.

  3. #3
    Join Date
    Nov 2003
    Posts
    1,405

    Re: Inheritance with C

    The best of course is to use an OO language but if you for some reason cannot, this article maybe can give you some hints,

    http://www.embedded.com/97/fe29712.htm

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Inheritance with C

    It's definitely possible, GTK+ does it.

    I get the impression that it's accomplished there by requiring the coder to cast to the base type that actually contains the desired functionality; but the casting is done using a macro that includes some sort of validity check. I don't know the details, but the system seems to work pretty well.

  5. #5
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Inheritance with C

    What do you mean by inheritance.

    One thing you can do is simply use composition:

    Code:
    struct X
    {
        int a;
    };
    
    struct Y
    {
        struct X x;
        int b;
    };
    
    void f(struct X* x)
    {
    }
    
    void g(struct Y* y)
    {
        f( &(y->x) );
    }
    My hobby projects:
    www.rclsoftware.org.uk

  6. #6
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Inheritance with C

    You could always have a base structure that contained pointers to functions. Each new 'derived' structure could be created by assigning different functions addresses to these pointers, thereby creating function overrides. There could be several 'creator' functions whose job it was to assign particular sets of functions to a structure.

  7. #7

    Re: Inheritance with C

    Pretty well any virtual machine for an object oriented language will implement inheritance using C.

    Vtables are not the only dispatch mechanism, you can create big switch statements, or combine vtables with a couple of cached cases at the dispatch site.

    How the type is encoded varies from vtable pointer, pointer to a type struct in the struct, or implicit schemes, such as allocating different small objects from different heap pages and manipulating the pointer to get the type stored at the heap base (eg, with 4K pages, ((type_t*)((size_t)p)&~(size_t)0xfff)) would give the type data for p, unless p == p &~0xfff in which case p points to a type).

  8. #8
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Inheritance with C

    Regarding virtual functions, this article called "COM in plain C" might help : http://www.codeproject.com/KB/COM/com_in_c1.aspx
    My hobby projects:
    www.rclsoftware.org.uk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured