It seems easy to make sure that an object can only be created on the stack and not on the heap (by simply marking private operator new), but is it possible to do the opposite: only permitting the creation of an object on the heap (with operator new) and *not* on the stack, with compiler warnings for the forgetful programmer?
trantran
0
Junior Poster in Training
Recommended Answers
Jump to PostIf you have something like a factory pattern then you can enforce it, for example:
class CandyFactory{ private: struct ChoclateBar(){ int calories; ChoclateBar(): calories(){} }; public: shared_ptr<ChoclateBar> makeChoclateBar(){ return shared_ptr( new ChoclateBar() ) } };
I don't know if thats what you were asking?
All 4 Replies
mrnutty
761
Senior Poster
mike_2000_17
2,669
21st Century Viking
Team Colleague
Featured Poster
trantran
0
Junior Poster in Training
mike_2000_17
2,669
21st Century Viking
Team Colleague
Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.