Reputation: 1074
If a C++ POD class inherits a class that does nothing except provide class-specific memory (de)allocation methods, then does that make the POD class non-POD?
For example, with
struct Myalloc
{
static void* operator new(size_t size);
static void operator delete(void* p);
// ... complete the set of operators
};
struct X : Myalloc {
int y;
double z;
};
is X
a POD type? It is a POD type if it doesn't inherit Myalloc
.
Also, do I need to define a virtual destructor for Myalloc
? I expect not, because Myalloc
provides no data members and only static methods.
Do the answers depend on which C++ standard (e.g., C++03, C++11, C++17, ...) is used? Our code needs to compile on multiple operating systems so we must write to the lowest C++ standard supported between them, which is C++03 with parts of C++11.
The reason for these questions is that I (unfortunately) need to make our own classes/structs use our own memory allocation routines, and it's a bad idea to globally replace the default memory allocation routines. Our code base has ancient roots and some of it still uses realloc
to extend the size of a buffer holding a sequence of POD values. If that POD type turns non-POD when I make it inherit Myalloc
, then I expect I'll need to explicitly construct the non-POD values, rather than just allocating memory for them. This would mean a full redesign of those ancient parts of the code, rather than just replacing realloc
with our own version. I cannot afford the considerable additional time for a full redesign right now.
Upvotes: 4
Views: 44
Reputation: 180945
Your derived class will not be a POD type. A POD type is defined in C++03 as
9(4): "A POD-struct is an aggregate class that has no non-static data members of type non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-define copy operator and no user-defined destructor. Similarly a POD-union is an aggregate union that has no non-static data members of type non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-define copy operator and no user-defined destructor.
Since it is defined as a aggregate if we look at how that is defined we have
8.5.1(1): "An aggregate is an array or class (clause 9) with no user-declared constructors (12.1), no private or protected non-static data members (clause 11), no base classes (clause 10) and no virtual functions (10.3)."
and we can see that it explicitly disallows having a base class. This means you cannot have POd type that is derived from some other type, not matter if that type is POD or not. C++11 does not change this as aggregates still cannot have base classes.
Upvotes: 3