A
Alf P. Steinbach
* Hicham Mouline:
It mixes in irrelevant concerns: platform-specific shared libraries,
singleton-with-arguments. Both of which are quite complex.
Anyway, in the above about the only thing a smart pointer could help with would
be exception safety in the 3rd party application, ensuring that it would call
finalize.
And for that you'd not change the 'adapter' pointer into a smart pointer (it
doesn't matter for the 'adapter' pointer, it's just a case of personal
preference),. You would instead provide a higher level interface (inline in a
header) that gives the 3rd party application a smart pointer. So the example is
an example where a smart pointer would be useful, but not as a simple
replacement of the current raw pointer.
Cheers & hth.,
- Alf
It is interesting to see so many different opinions about this.
Perhaps, the discussion can be fine tuned if I put down the use case
(trivial one) here:
We have a 3rd party application that accepts to load shared dynamic
libraries, this is on windows.
The application requires a number of entry points into the DLL. These are
"exported" functions, 2 of these
are
// with C linkage convention (ie , they use C and not C++)
int initialize(some_type* f);
void finalize();
1 translation unit where we implement this requirement looks like
extern "C" {
static const our_adapter* adapter=0; // global static raw pointer
int initialize(const some_type* f)
{
if (f==0)
{
return failure; /// 3rd application will ignore this dll
}
adapter = new our_adapter(*f); /// ctor never fails. If new fails, the
3rd party application crashes (we decided it's ok)
}
//// we axiomatize that initialize() is called once before finalize() and
that the 3rd party application calls these just once
void finalize()
{
delete adapter;
}
// other required function
int other_function()
{
adapter->do_something();
}
}
I would have had as a global variable the our_adapter itself, but its ctor
requires the "f", so I used a pointer instead.
Because of the exisiting symmetry between initalize and finalize, new'ing
and deleting seem to me appropriate.
My colleague insists I should use auto_ptr<our_adapter> instead of a raw
pointer here, and then said as a general rule,
never use a raw pointer when the smart pointer will work, hence the post.
could I have opinions about this particular case?
It mixes in irrelevant concerns: platform-specific shared libraries,
singleton-with-arguments. Both of which are quite complex.
Anyway, in the above about the only thing a smart pointer could help with would
be exception safety in the 3rd party application, ensuring that it would call
finalize.
And for that you'd not change the 'adapter' pointer into a smart pointer (it
doesn't matter for the 'adapter' pointer, it's just a case of personal
preference),. You would instead provide a higher level interface (inline in a
header) that gives the 3rd party application a smart pointer. So the example is
an example where a smart pointer would be useful, but not as a simple
replacement of the current raw pointer.
Cheers & hth.,
- Alf