Re: inhibit compiler warning C4624 for a class hierarchy
"Igor Tandetnik" <itandetnik@mvps.org> wrote in message
news:eWn8hhaOHHA.4928@TK2MSFTNGP06.phx.gbl...
Ben Voigt <rbv@nospam.nospam> wrote:
Just to be sure, is it standard-compliant to cast raw memory returned
by malloc(sizeof TOp) to TOp?
Yes, unless TOp is a class type with a non-trivial constructor (3.8/1).
Note that an explicitly declared constructor, even an empty one, is
non-trivial (12.1/5)
What if it's explicitly declared and never defined (classic trick for
preventing constructors in derived classes)? I just checked, and
__has_trivial_constructor(TOp) is false. If a class does not have a trivial
constructor, does it necessarily have a non-trivial constructor, or can it
have no constructor at all?
struct PNPEXPORT IConcurrentOperations::OpMessage abstract
{
friend void* IConcurrentOperations::alloc_helper(size_t);
private:
/**
** \brief custom allocator for operational messages
**
** \param[in] bytes number of bytes requested
** \return pointer to a block of (at least) size bytes, or NULL if
insufficient memory if available
**/
static void* alloc(size_t bytes)
{
return malloc(bytes);
}
private:
// prevent creation of objects of subtypes, because
// virtual destruction is impossible
OpMessage() throw();
OpMessage(const OpMessage&) throw();
void* operator new(size_t) throw();
void operator delete(void*) throw();
public:
size_t reservedbytes() const { return 0; }
protected:
/**
** \brief custom deallocator for operational messages
**
** \param[in] p pointer to memory block to be freed
**/
static void dealloc(void* p)
{
free(p);
}
};
inline void* IConcurrentOperations::alloc_helper( size_t bytes )
{
return OpMessage::alloc(bytes);
}
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925