1

I am working on a small library, where I have following requirements for any class X:

  1. class X must be allocatable only using operator new
  2. All the children of class X should implicitly become allocatable only by operator new
  3. The syntax for heap allocation should be elegant
  4. Not much of the existing code should be changed to incorporate this
  5. After some point of time, if I want to allow class X to be allocatable as automatic; again not much code should be changed

I also welcome C++0x solutions (for future use only).

[Note: I have done my part of research and will be posting it as an answer (tested for basic scenarios)]

iammilind
  • 68,093
  • 33
  • 169
  • 336
  • possible duplicate of [Is it possible to prevent stack allocation of an object and only allow it to be instiated with 'new'?](http://stackoverflow.com/questions/124880/is-it-possible-to-prevent-stack-allocation-of-an-object-and-only-allow-it-to-be-i) – cdhowie Aug 13 '11 at 07:38
  • 2
    @iammilind: If you already have an solution, Are you posting this as an test for others? If not, post your approach and then ask for comments that critic or support it. – Alok Save Aug 13 '11 at 07:38
  • What do you mean by "not much existing code should be changed"? We don't know what your existing code is, so we can't tell you how much change there will be. – foxy Aug 13 '11 at 07:41
  • @cdhowie, that is not duplicate. I am not getting my answer there. My requirements are different and I have listed them properly. – iammilind Aug 13 '11 at 07:45
  • @Als, No I am not testing others; I wanted to have ideas from others. I already mentioned that will put my solution as one of the answers (as there can be better than that too). Which I have posted. – iammilind Aug 13 '11 at 07:46
  • @iammilind: Voting to close as `Not a Real Question`, There is a Question and it has an answer, there is no real question, Also it does not fit the `C++-Faq` Ideology, Since this question is not a FAQ by anyways. – Alok Save Aug 13 '11 at 07:49
  • @Als, well I see so many questions for asking design methodologies. I don't know why it doesn't fit as an FAQ,(if it can be a possible duplicate of already existing question). – iammilind Aug 13 '11 at 07:51
  • your requirement 1. says class X `must` be allocatable *only* using operator new. and then in the requirement 6 you want the `class X` to be allocatable as a `automatic`. – A. K. Aug 13 '11 at 07:56
  • @Aditya, Requirement 6 actually says that, in case after some point of time during the development phase, I learned that actually `class X` should be allowed to be an automatic then, it should also be easier (again not much changes to be reverted). – iammilind Aug 13 '11 at 07:59
  • your requirements are pretty close to a `factory pattern` except for the last requirement i guess. – A. K. Aug 13 '11 at 08:04

1 Answers1

3
// Dynamic.h
class OnlyDynamic
{
  template<class T> friend struct Dynamic;
  virtual void*** __Restriction () = 0;
};

template<class T>
class Dynamic : public T
{ 
  virtual void*** __Restriction () { return 0; }
  ~Dynamic();
public:
#ifdef Cpp0x
  template<typename... Args>
  Dynamic(Args... args) : T(args...) {}
#else
  Dynamic () {}
  template<typename A1> Dynamic(const A1 &a1) : T(a1) {}
  template<typename A1, typename A2> Dynamic(const A1 &a1, const A2 &a2) : T(a1,a2) {}
//...
  template<typename A1, typename A2, typename A3, typename A4, typename A5, typename A6>
  Dynamic(const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4, const A5 &a5, const A6 &a6) : T(a1,a2,a3,a4,a5,a6) {}
#endif 
};

Usage:


Suppose, I want to make class X only dynamically allocatable; I should simply derive OnlyDynamic (access specifier doesn't matter) and allocate with new Dynamic<X>().

Example:

class Base {};

struct A : Base, OnlyDynamic  // <-- only inherit
{
  A (int i) {}
};

A *p = new Dynamic<A>(3);
delete p;

As of now, I am seeing all the given requirements getting fulfilled with this solution.

iammilind
  • 68,093
  • 33
  • 169
  • 336