Topic: Re:Can STL vectors handle pointers?
Author: scherrey@proteus-tech.com (Benjamin Scherrey)
Date: 1995/07/12 Raw View
In message <3tsp4u$b1o@metro.ucc.su.OZ.AU> - maxtal@Physics.usyd.edu.au (John
ax Skaller) writes:
:>
:>In article <3tkutu$q34@sparco.objectSpace.com>,
:>Graham Glass <gglass@objectSpace.com> wrote:
:>>In article <3sspfv$j3i@darkstar.UCSC.EDU>, ray@cse.ucsc.edu (Ray Swartz) says:
:>>>
:>>>I have a class hierarchy that looks like this:
:>>..........
:>>>I got an error inside one of the STL component libraries.
:>>>
:>>>Is it possible to put pointers into an STL container?
:>>>
:>>>Ray Swartz
I cut out a lot of stuff as I don't have the original posting...
Here is some code that required me to create a set of DeleteHoles (a
class of mine) and a multiset of DeleteHole*'s. This is done using the HP STL
code. The trick is that the template code for the destroy and construct
function overloading produces bad code for pointer types so you have to
explicitly define them as follows:
-------------------------------- cut here ------------------------------
#include "stl.h" // HP STL Library Headers
#include "delete.hpp" // DeleteHole Class
//
// destroy() and construct() have to be explicitly defined here as
// the pointer types do not template well for user-defined classes.
//
// Is there any way around this?!?!?
//
inline void destroy( const DeleteHole** pointer )
{
(*pointer)->~DeleteHole();
}
inline void construct( const DeleteHole** p, const DeleteHole* value)
{
new (p) (const DeleteHole*)(value);
}
struct less_pos
{
bool operator()(const DeleteHole& x, const DeleteHole& y ) const
{
return (x<y);
}
};
struct less_size
{
bool operator()(const DeleteHole* x, const DeleteHole* y) const
{
return x->isFit( *y ); // bool DeleteHole::isFit( const DeleteHole& )
// const;
// is how this method is declared.
}
};
typedef set< DeleteHole, less_pos > Positions;
typedef multiset< const DeleteHole*, less_size > Sizes;
-------------------------------- cut here ------------------------------
Hope this helps you out. So far it's been working quite well for
me. I don't know if this works outside of the HP code and would
appreciate hearing any comments from other developers. Thanx go to
several comp.std.c++ participants who previously helped me figure this
stuff out!
later,
Ben Scherrey
scherrey@proteus-tech.com
//
// Benjamin Scherrey
// Proteus Technologies, Inc.
// (404) 454-1013v (404) 986-9876f
//