By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,385 Members | 1,737 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,385 IT Pros & Developers. It's quick & easy.

Wrapper objects

P: n/a
I would like to know more about C++ wrappers. What is a wrapper object
in C++ and why is it neccessary? I am trying to create a templated
linked list data structure to store any object or primitive however I'm
told it would work best with a wrapper object. I did a google search
and came up with a java
definition(http://javaalmanac.com/egs/java.lang/Wrap.html) would it
work for C++ the same way?

Nov 10 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
ni*****@gmail.com wrote:
I would like to know more about C++ wrappers. What is a wrapper object
in C++ and why is it neccessary?
A wrapper object is an object that acts as a wrapper round another
object. That could mean almost anything.

I am trying to create a templated linked list data structure to store any object or primitive however I'm
told it would work best with a wrapper object. I did a google search
and came up with a java
definition(http://javaalmanac.com/egs/java.lang/Wrap.html) would it
work for C++ the same way?


No it would not.
First you need to clarify your requirements. Do you mean a list which
can hold objects of one single type (a homogenous list)? Well C++
already has that. Or do you mean a list which can hold different types
simultanously (a heterogenous list)? If that is what you mean then where
does the template fit in?

Can't help until you answer this fundamental question.

john
Nov 10 '05 #2

P: n/a
ni*****@gmail.com wrote:
I would like to know more about C++ wrappers. What is a wrapper object
in C++ and why is it neccessary? I am trying to create a templated
linked list data structure to store any object or primitive however I'm
told it would work best with a wrapper object. I did a google search
and came up with a java
definition(http://javaalmanac.com/egs/java.lang/Wrap.html) would it
work for C++ the same way?


A wrapper object is probably a reference to using the constructor and
destructor for automatic and exception safe resource allocation and
deallocation. I'm not sure what your source means by suggesting it, but
perhaps s/he was thinking of something like boost::shared_ptr to hold
elements if they are pointers (this doesn't really apply if the objects
are not pointers).

See Stroustrup's paper on the subject of wrapping:

http://www.research.att.com/~bs/wrapper.pdf

BTW, there's already a templatized linked list in the STL, and you
shouldn't roll your own unless you have to (e.g. for a data structures
class).

Cheers! --M

Nov 10 '05 #3

P: n/a
ben
ni*****@gmail.com wrote:
I would like to know more about C++ wrappers. What is a wrapper object
in C++ and why is it neccessary? I am trying to create a templated
linked list data structure to store any object or primitive however I'm
told it would work best with a wrapper object. I did a google search
and came up with a java
definition(http://javaalmanac.com/egs/java.lang/Wrap.html) would it
work for C++ the same way?

A wrapper object most likely would act on behalf of the object in wraps,
in a way that would make accessing the wrapped object:
possible (e.g. if the wrapped object is somewhere on the Internet);
easier (e.g. if the wrapped object has a crumblesome interface);
safer (e.g. keeps locks, reference counting, etc);
faster (by utilizing optimized operation, by caching last result, etc);
recordable (bookkeeping debug information, records exception, etc);
abstract (by doing away some unnecessary operations and so make it
compliant to other similar objects);
more flexible (e.g. the damage done by changing the public interface of
the wrapped object is contained);
more managible (e.g. if the wrapper does give a simpler interface, etc.)

There are also cases when a wrapper object wraps around an interface
that does not explicitly use C++ class objects. These wrappers
simplifies, encapsulates, restructures, refactors and in some cases
adapts the wrapped interface to suit your needs.

Ben
Nov 10 '05 #4

P: n/a

<ni*****@gmail.com> a écrit dans le message de news:
11**********************@g44g2000cwa.googlegroups. com...
I would like to know more about C++ wrappers. What is a wrapper object
in C++ and why is it neccessary?
Example:
I develop algorithms to do image processing. I can use a few image
processing library to help me
do several basic operations on images. lets say I have 2 library A and B and
each of them have a function
to transform a color image to grey scale.

Now the 2 available functions (one in library A and 1 in B) dont share the
same parameters list and dont even share
the same name. But I want to flexibility to easily use eigther the function
from A or B. So I make a wrapper
around the 2 functions. Basically its a function that can call library A or
B and it deal with the details like the possible
diffenrence in the parameters list.

Once you have wrapped everything you want, its now easy to add a third
library or to kill one you dont want anymore,
since you always deal with generic functions, and never a library specific
interface.

Eric

I am trying to create a templated
linked list data structure to store any object or primitive however I'm
told it would work best with a wrapper object. I did a google search
and came up with a java
definition(http://javaalmanac.com/egs/java.lang/Wrap.html) would it
work for C++ the same way?

Nov 10 '05 #5

P: n/a
Here is another way to look at it. A wrapper is simply a method or
class that delegates traffic to another method or class.

Consider a DLL. A DLL can only expose methods - not objects
themselves. If I want to have a class object in a DLL, you have to
expose methods that are public from outside the DLL. Those methods
would then delegate the calls to the inner class object methods. The
exposed DLL methods would then be called wrapper methods.

Here is another example. Let's take Java for instance. Java runs on
top of a JVM meaning it is garbage collected, etc. To communicate with
the outside world, Java uses what is called the JNI (or Java Native
Interfaces). All communication with the outside world (e.g. external
processes, system libraries, DLLs, etc.) must go through the JNI. A
common thing to do is to create "proxy" or "wrapper" objects that
mimick the functionality of the external libraries and handle all of
the JNI communication. That way, your application would just use the
wrapper objects without worrying how it works with the JNI below. Make
sense?

Nov 11 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.