热度 12
2013-6-21 18:46
3223 次阅读|
0 个评论
For some time now, I've been discussing how to implement and use virtual functions in C. , The basic approach is to implement each class as a C structure and implement each associated method as a function whose first parameter is a pointer to the structure. For each polymorphic class (a class with at least one virtual function), the corresponding C structure has a member called a vptr , which points to a table of function pointers called a vtbl . Last April, I showed how to mimic class inheritance in C in two distinct ways, which I call "inheritance by composition" and "inheritance by copying and editing." The two approaches differ in how they declare the vptrs and vtbls. I'm planning to show other variations, but not just yet. Rather, I want to pause and reflect on why I'm covering this topic. A few years ago, I wrote a series of columns on representing memory-mapped devices as objects in C and in C++. , Over the following year or so, I elaborated on the techniques, introducing other C++ features as appropriate. A few readers objected to my C++ implementations for failing to use virtual functions. This issue came up repeatedly, so I decided to address virtual functions in depth. My goal in writing this series was to explain what virtual functions are and how typical C++ compilers implement them. Most embedded.com readers use C rather than C++. Thus, I thought the best way to show how C++ typically implements virtual functions is to show how to implement virtual functions in C to get functionality and performance very similar to what you get from C++. I figured those of you already using C++ might learn more about how virtual functions work, and those of you using C might learn some techniques you can use in your code. I was also hoping to help some C users realise that C++ really is better for object-oriented programming. If you program in C, but you also want to do object-oriented programming, you probably should be using C++ instead of C. Yes, I know, some of you have constraints that prevent you from using C++. Some of you program on platforms for which no C++ compiler exists, or the available compilers don't mesh well with other tools you're using. In that case, you can approximate object-oriented features in C using techniques like the ones I've been describing. But don't kid yourself. Compared to C++, virtual functions in C are cumbersome and error-prone, and rarely offer any performance advantage. Indeed, C has a few features that C++ lacks, such as the restrict qualifier, which facilitates certain optimisations. But if you need these features, and your C++ compiler doesn't support them as extensions, you can write selected portions of your code in C. Needing these features is a poor reason to use C for everything, when C++ is so much better than C for object-oriented programming (as well as other things). Next time, I'll continue discussing the details of implementing virtual functions in C, not only because it's useful to know, but also because it touches on various other interesting programming topics. References 1. Saks, Dan, "Implementing virtual functions in C++," Eetindia.com, April 2012. 2. Saks, Dan, "Implementing virtual functions in C," Eetindia.com, August 2012. http://forum.eetindia.co.in/BLOG_ARTICLE_13544.HTM. 3. Saks, Dan, "Alternative ways to mimic inheritance in C," Eetindia.com, April 8, 2013. http://forum.eetindia.co.in/BLOG_ARTICLE_16744.HTM. 4. Saks, Dan, "Accessing memory-mapped classes directly", Eetindia.com , March 21, 2011. http://forum.eetindia.co.in/BLOG_ARTICLE_7042.HTM. 5. Saks, Dan, "Memory-mapped devices as C++ classes", Eetindia.com , March 24, 2011. http://forum.eetindia.co.in/BLOG_ARTICLE_7022.HTM. 6. Saks, Dan. "Employ member new to map devices," Eetindia.com, November 25, 2011. http://forum.eetindia.co.in/BLOG_ARTICLE_10437.HTM. 7. Saks, Dan. "Unexpected trends," Embedded System Design, May 2, 2012. www.embedded.com/4372180.