c++.stlsoft - Enforce new?
- Ilya Minkov (9/9) Mar 12 2005 Hello.
- Matthew (73/82) Mar 12 2005 Hide the ctor and use a Create method (with the same signature as the
Hello. I want to forbid for some of my classes to be instantiated on the stack - because the constructor stores the this pointer immediately in a searchable static container and the objects need to keep alive for most of the duration of the program. Are there any ideomatic means of doing that? If that's not a oneliner, i would think that this might be a valuable addition to stlsoft libraries. -eye
Mar 12 2005
Hide the ctor and use a Create method (with the same signature as the
ctor). I use this fairly regularly.
Unfortunately, it's almost impossible to get a workable generic version
with the current language, due to the hassles with
const/pointer/reference/value differences with argument forwarding (as
described in Chapter 23 of your copy of IC++).
The upside is that it is a nice point at which to introduce shims into
the construction process, which can simplify client code a great deal.
Here's a snippet from a recent project (with names changed to protect
client's IP):
class BaseClass;
. . .
class Auditor;
class Handler;
. . .
typedef ::stlsoft::shared_ptr<BaseClass> BaseClass_ptr;
. . .
class SomeClass
: public BaseClass
{
/// \name Types
/// {
public:
typedef BaseClass parent_class_type;
typedef SomeClass class_type;
/// }
/// \name Construction
/// {
private:
/// Default constructor
SomeClass( Handler *handler
, char const *processIdentity
, char const *channelIdentity
, char const *auditPoint
, Auditor &auditor);
public:
/// Destructor
~SomeClass();
static BaseClass_ptr CreateInstance( Handler *handler
, char const
*processIdentity
, char const
*channelIdentity
, char const *auditPoint
, Auditor &auditor);
template <typename S1, typename S2, typename S3>
static BaseClass_ptr CreateInstance( Handler *handler
, S1 const
&processIdentity
, S2 const
&channelIdentity
, S3 const &auditPoint
, Auditor &auditor)
{
return class_type::CreateInstance( handler
,
::stlsoft::c_str_ptr(processIdentity)
,
::stlsoft::c_str_ptr(channelIdentity)
,
::stlsoft::c_str_ptr(auditPoint)
, auditor);
}
/// }
. . .
};
Thus, one can call CreateInstance() with any types for which string
access shims are defined and available.
In this case, the instance is passed into a shared_ptr, but this 'idiom'
(if it qualifies as an idiom) works just as well returning pointers,
should that be appropriate.
"Ilya Minkov" <minkov cs.tum.edu> wrote in message
news:d0v9cl$1i3$1 digitaldaemon.com...
Hello.
I want to forbid for some of my classes to be instantiated on the
stack - because the constructor stores the this pointer immediately in
a searchable static container and the objects need to keep alive for
most of the duration of the program.
Are there any ideomatic means of doing that?
If that's not a oneliner, i would think that this might be a valuable
addition to stlsoft libraries.
-eye
Mar 12 2005








"Matthew" <admin stlsoft.dot.dot.dot.dot.org>