I'm a little confused about how to go about creating a wrapper class? I think I need to start with the basics. What I want to do is write a C++ wrapper class for a C API. I know several of you could explain this well but I need to go right to the basics on this kind of thing. Was wondering if anyone out there who knows any books which explain this kind of thing well? Maybe a book explaining several programming concepts as well as the idea of creating a wrapper class. I have plent of time to learn this.

It's not necessarily a C++ wrapper class for a C API which is important here but it's the general approach to doing this which is important to me. It would certainly be useful if there is something out there which specifically refers to making a C++ wrapper.

I hope this question is not too broad. No examples in this question (my question), just want to get the hang of how to go about something like this.

Thanks so much

4 Years
Discussion Span
Last Post by daino

There really aren't any specific rules about making wrapper casses I'm afraid; it depends too much on the functions and structures which are being encapsulated. Some wrapper classes are nothing more than a static class with a group of methods that call the original functions directly being nothing more than a way of organizing the existing functions into a single package. Others focus on building a class around a data structure, with the aim of simplifying the function calls by making them methods of the data structure, rather than having to apply the functions to the data. Still others may actually combine behaviors in ways that actually change the way the library works, though at that point you generally wouldn't call it just a wrapper.

The closest thing to a general method or approach which I've found was this article, and I'm not sure it really applies generally. TheAdapter Pattern is often seen as a type of wrapper, but it isn't necessarily done as a wrapper class per se, IIUC.

Edited by Schol-R-LEA


A wrapper class is a fairly broad concept. It basically means to repackage a piece of code into a new interface (or coating). That I can think of right now, there are two main reasons for doing a wrapper class: adapting an interface, and decoupling the implementation from the interface (i.e., most often called a "compilation firewall"). Then, you can talk about a thin wrapper which just means that the wrapper class barely just forwards function calls to some underlying implementation (e.g., a C API), but some wrappers can get thicker and thicker in which case you would probably stop calling it a wrapper.

An adaptor is used to take a given class (or set of API functions) and make it look like something else, usually something that is needed by another library (or your own). Good and simple examples of that are the STL adaptors like std::queue, std::stack, or std::priority_queue, they simply take any STL container (like std::vector or std::list) and makes it look like a queue or stack. In this case, the classes std::stack and std::queue would be considered thin wrappers, while std::priority_queue would be considered to be a bit thicker (almost too thick to be called a wrapper, because of its heap-structuring code, but this is still an adaptor nonetheless). The purpose of adaptors is to adapt a piece of code to make it usable in another piece of code that expects a slightly different interface (or functionalities).

A compilation firewall is a different beast. Again, it's a type of wrapper (could be thin or thick) that is used for the purpose of stashing away the implementation details. A typical example of this is when you are writing cross-platform code. Say that you are writing some code to send some packets over TCP/IP, and you want to compile for both Windows and Linux which use different APIs for that purpose. Then, you would write two classes that wrap all the OS-specific calls to send/receive packets and expose the same interface (set of functions). This way all the rest of the code will only have to interact with that interface, i.e., the interface acts as a firewall between the implementation details of the TCP/IP API used and your code that sends and receives packets. And, to do this well, you normally make it so that you can compile the two separately, so it no longer is just a conceptual firewall but a compilation firewall. In any case, this is a bit more advanced stuff. Under most circumstances, people use the terms "wrapper" and "adaptor" almost synonymously.

There is no definite recipe for creating a wrapper class, it is just a product of trying to glue together different libraries or different parts of a library. It is very much a purpose-driven thing. If you need a component to have a particular interface such that it can work with some piece of code or library, but you want to reuse a component that you already have (or grabbed from another library) and don't want to modify it, then you write a wrapper to make the link between the two. Very often people see wrappers (or adaptors) as just glue between two interfaces.

N.B.: Ideally, libraries should be designed with non-intrusive interfaces, making the need for wrappers moot.
