Thread: Simulating Inheritance in C

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    167

    Simulating Inheritance in C

    To simulate inheritance in C what would be the better solution?

    1. Use the base Structure as a member of the derived one?

    2. Copy and paste the declarations for the base structure into the derived structure?

    3. Other option I haven't thought of?

    Any links in general to OOP with C?
    silk.odyssey

  2. #2
    Registered User Boomba's Avatar
    Join Date
    Jun 2003
    Posts
    89
    I would guess 2..seems tediouse ofcourse....which is why C++ exsists

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by silk.odyssey
    Any links in general to OOP with C?
    Here's one:
    http://ldeniau.home.cern.ch/ldeniau/html/oopc/oopc.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I generally use option 1. Something like:
    Code:
    struct foo
    {
      int somedata;
      int somemoredata;
    };
    
    struct bar
    {
      struct foo basedata;
      int additionaldata;
    };
    If you understand what you're doing, you're not learning anything.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You could just choose not to do this with C, but you're on the right track. Do a bit of Google searching to find answers to specific questions, but I found a decent article that will get you started.

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    167
    Thanks for the tips and the links. I am not thinking of doing anything too complex though. For that learning C++ would be a better option for me. I would prefer avoiding c++ because i prefer simpler languages. With c++ there are too many rules and exceptions to learn. With C I think I will be better able to understand what the code does. What I want is not necesarily to implement C++'s object model in C but maybe to think in an Object oriented way while coding in C. I think structures for encapsulation and void pointers for polymorphism would work well enough. I don't want the code to get too complicated because that would make c++ a better option.
    silk.odyssey

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  4. Inheritance and Polymorphism
    By bench386 in forum C++ Programming
    Replies: 2
    Last Post: 03-18-2004, 10:19 PM
  5. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 04:41 AM