Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of a C++ Wrapper Class?

Tags:

c++

class

wrapper

I have a little trouble in understanding a wrapper class. It would be great if some one could help providing apt examples.

  1. What is a C++ Wrapper Class and what are the circumstances of writing it ?
  2. What is it's use any way ?

Thanks.

like image 724
Mahesh Avatar asked Mar 15 '11 03:03

Mahesh


People also ask

What is meant by a wrapper class?

A Wrapper class is a class which contains the primitive data types (int, char, short, byte, etc). In other words, wrapper classes provide a way to use primitive data types (int, char, short, byte, etc) as objects. These wrapper classes come under java. util package.

What is a wrapper function C++?

A wrapper function is a subroutine (another word for a function) in a software library or a computer program whose main purpose is to call a second subroutine or a system call with little or no additional computation.

Why are wrapper classes used?

Use of Wrapper Class in Java Programming The wrapper class implements the technique to convert the primitive into object and object into primitive. There is a concept of autoboxing and unboxing in the wrapper class, which transform the primitives into objects and objects into primitives automatically.

What is a wrapper class give two examples?

Wrapper class Example: Primitive to Wrapper //Java program to convert primitive into objects. //Autoboxing example of int to Integer. public class WrapperExample1{ public static void main(String args[]){ //Converting int into Integer.


2 Answers

A "wrapper class" is a de facto term meaning a class that "wraps around" a resource; i.e, that manages the resource. When people write a wrapper, then, they are doing something like this:

class int_ptr_wrapper { public:     int_ptr_wrapper(int value = 0) :     mInt(new int(value))     {}      // note! needs copy-constructor and copy-assignment operator!      ~int_ptr_wrapper()     {         delete mInt;     }  private:     int* mInt; }; 

This class manages ("wraps") a pointer to an int. All resources should be wrapped in some fashion, for cleanliness (no explicit clean up code or noise) and correctness (destructor is guaranteed to run; cannot forget to clean up, and safe with exceptions).

This pattern is called Scoped-bound Resource Management (SBRM), though a far more common (but most esoteric) name is Resource-Acquisition is Initialization (RAII). The idea is to bind a resource's clean-up to a destructor, for the reasons given above: the scope handles the rest.

Note that I said it was missing a copy-constructor and copy-assignment operator. This is due to the Rule of Three. (See linked question for detailed explanation.) The simplest way to correctly implement this rule is with the copy-and-swap idiom, explained here.


Sometimes, it's not pragmatic to write wrapper class for resource clean-up, usually when the resource is unique or used once. (Or with transactional programming.) The solution to this is called scope guard, a way of writing clean-up code inside the function that needs it.

You may find more information by searching for it in your favorite search provider (that is, Google), or going to the "primary" document here. Note that Boost provides a utility for this, as it usually does for good idioms.

like image 76
GManNickG Avatar answered Oct 06 '22 00:10

GManNickG


A wrapper is just some smallish class whose purpose is to provide a different interface than the thing it wraps. For example, it is common to take a C API and write one or more classes that "wrap" it to provide an object-oriented interface rather than a procedural one.

like image 28
John Zwinck Avatar answered Oct 06 '22 01:10

John Zwinck