Objectives:
Example:
#include "Base.h"
#include "Derived.h"
{
static Base sb; // OK
Base ab, *hb = new Base; // OK
static Derived sd; // OK
Derived ad; // OK
Derived *pd = &ad; // OK
Derived *hd = new Derived; // Compile error, link error,
// test-unit exception, or lint gripe
struct Composite {
Base cb;
Derived cd;
} *hc = new Composite; // OK
// Edit to show side-effects of solutions which hide Base::operator new.
std::vector<Base> vb; // OK
std::vector<Derived> vd; // Error
// ...
}
How could Base be implemented to achieve this? A compile-time error is preferred to a link-time error; but although both are preferred to a test-unit exception, and a test-unit exception is preferred to a lint gripe, any solution that does not require an update to Base.h for each derivation will do.
Edit: For the purposes of this question, solutions which involve forking the compiler so that it supports arbitrary decorations are, despite the technical challenge, classified as "trivial."
Hmm, Eclipse's answer is gone, but I thought it was on the right track.
class Base {
public:
static Base *create() { return new Base; }
static Base *create(size_t n) { return new Base[n]; }
private:
// Prevent heap allocation
void *operator new(size_t s);
void *operator new[](size_t s);
};
This isn't nice, as it obligates you to use Base::create()
instead of new Base
, and class Derived
could still go and implement its own public operator new
, but I think it's workable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With