By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,137 Members | 1,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,137 IT Pros & Developers. It's quick & easy.

how can we restrict object creation on heap ?

P: n/a
how can we restrict object creation on heap ?

soln1: overload new

any other way to do this ?

Dec 22 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

shaanxxx wrote:
how can we restrict object creation on heap ?

soln1: overload new

any other way to do this ?
The only way I can think is to create your own memory pool and check
the address of the object is in it in the constructor. OTOH maybe you
can get at the heap top and bottom pointers for a similar result. Not
sure how to do that portably though.

regards
Andy Little

Dec 22 '06 #2

P: n/a
shaanxxx wrote:
how can we restrict object creation on heap ?
I think you mean require class instances to be in heap.
>
soln1: overload new
Wrong, unless I misunderstood above.
>
any other way to do this ?
Easy, portable way: make all constructors
private, then provide a static member that
creates an instance on the stack, and
returns a pointer, like X::make_inst for
example:

static X * make_inst() { return(new X); }

Dec 22 '06 #3

P: n/a

wk****@yahoo.com wrote:
shaanxxx wrote:
how can we restrict object creation on heap ?

I think you mean require class instances to be in heap.

soln1: overload new

Wrong, unless I misunderstood above.

any other way to do this ?

Easy, portable way: make all constructors
private, then provide a static member that
creates an instance on the stack, and
returns a pointer, like X::make_inst for
example:

static X * make_inst() { return(new X); }
You pay quite a high price just to ensure construction only on the
heap...

class X{
private:
X(){}
friend X* make_X(){return new X();}
protected:
virtual void f(){};
};

class Y : protected X{
public:
Y() : X(){}
void f(){}
};

int main()
{
Y y;
}
regards
Andy Little

Dec 22 '06 #4

P: n/a
wk****@yahoo.com wrote:
shaanxxx wrote:
how can we restrict object creation on heap ?

I think you mean require class instances to be in heap.

soln1: overload new

Wrong, unless I misunderstood above.

any other way to do this ?

Easy, portable way: make all constructors
private, then provide a static member that
creates an instance on the stack, and
returns a pointer, like X::make_inst for
example:

static X * make_inst() { return(new X); }
An alternative:

If the objective is to *solely* limit object creation to use the heap,
it may be easier at times to declare the destructor as private instead
of the constructor. If the class has many constructors, you must
remember to make all the constructors private. This also includes both
the copy and default constructors if they would otherwise be generated
by the compiler.

If you just declare the destructor private, all you need is to declare
one method private, and provide a psuedo-destructor that simply deletes
the object...

Dec 22 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.