Inheritence and C
Is there any kind of inheritence in C... wait with the flame.... Ok, I mean, there is some OO in C, structs, C++ classes are almost as structs but (not default public n some other stuff). and structs let you do a lot of OO stuff like Methods and members and having instances. but is there anyway you could have inheritence in ANSI C, using any technique including #define and such. ?????
- now you can flame me.
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~I'm looking for work
No. Inheritence requires the ability to inherit base type into a derived type, but still being able to appear outwardly as the base type. C++ uses virtual member functions along with a static virtual function table that is generated by the compiler for each derived class to accomplish this. C lacks this feature.
You can fake inheritence by composing a struct with another struct, and by using a table of function pointers that will be set depending on how you want your class to act, and a lot of funky casting of your struct into a different type. However, it takes a lot more work to do than if you were using C++.
Embedded Systems magazine had an article about faking inheritence in C using these techniques a while ago. You might want to look it up.
You can fake inheritence by composing a struct with another struct, and by using a table of function pointers that will be set depending on how you want your class to act, and a lot of funky casting of your struct into a different type. However, it takes a lot more work to do than if you were using C++.
Embedded Systems magazine had an article about faking inheritence in C using these techniques a while ago. You might want to look it up.
You might want to check out the book Object Oriented Programming in C. Here''s the link to the PDF version. Link. But I don''t recommend using OO in C. It''s just a major pain.
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement