On Sat, 3 Dec 2005 11:04:11 -0500, "Tom the Canuck"
<tv***@sympatico.ca> wrote:
What would be the best way to proceed?
Should I make a pure virtual class and then
derive from that? I want the base class to have
functions defined so that I don't have to do the
work all over again in the derived class. Is this
not what objects are for?
Please illuminate on how to do this properly.
I don't want code, just a pointer on how to
do the job right.
Any comments or links will help.
The C code is OpenGL. I just want a class
to make life easier for future projects.
If requested, I can send the source code done
up to now. I do not believe in wasting space, so
it is not in this post.
Thanks in advance,
Tom the Cunuck.
As others have pointed out, there is no "best answer" here. But it
would be interesting to point out in very general terms what
advantages C++ can give you when wrapping a set of C functions which
together constitute some kind of system or library.
Let's start with some of the more important ones:
- RAII ("Resource Acquisition Is Initialization"). Design the C++
class so that it automatically (i.e. upon construction of an object)
allocates all the necessary memory for its members, possibly using
smart pointers for exception safety, and cleans up after itself on
destruction. This might or might not necessarily entail calling memory
allocation and de-allocation functions such as "new" and "delete", but
also other things such as initializing and freeing "handles" or
calling global C initialization and termination functions in the
third-party library. This, IMHO, is one of the areas where C++ really
shines.
- Type safety. Clients need not fool around with passing void pointers
to a function in some circumstances, but casting the same argument to
unsigned int in others and passing an additional "flag" argument so
that the function knows which type to expect (you can tell I'm talking
about ODBC here <g>). Take the all-purpose C library function and
provide several context-specific C++ member functions which pass the
appropriate types.
- Encapsulation. Hide your handles, buffers, etc. from the clients and
use STL containers such as std::string, std::vector, std::list, etc.
- Provide meaningful error parsing, logging and recovery by using C++
exception classes.
Well, I'm just getting started here ... but maybe some other people
would like to continue the list?
--
Bob Hairgrove
No**********@Home.com