problem realated to C++

V

Vijay

Hi All,

I am not able to solve one problem. Can any one help me?

Problem: write a program which restricts the creation of object in
stack. Means, program should allow a creation of object only using
"new" operator.

Thanks in advance.

Regards,
Vijay
 
M

Michael DOUBEZ

Vijay a écrit :
Hi All,

I am not able to solve one problem. Can any one help me?

Problem: write a program which restricts the creation of object in
stack. Means, program should allow a creation of object only using
"new" operator.

This looks like homework.
Look for Item 27 in "More Effective C++" by Scott Meyers.

If you don't have access to this book.
Hint: declare constructor protected and find a way to call it.

Michael
 
M

Mark P

Roland said:
Nope, "program should allow a creation of object only using "new"
operator":

A* p = new A; // ok
A a; // not ok

Depends how literally you want to interpret the OP's question. In any
event, AFAIK there's no way to satisfy your interpretation. True?
 
R

Roland Pibinger

Depends how literally you want to interpret the OP's question. In any
event, AFAIK there's no way to satisfy your interpretation. True?

Probably not for a compile time solution but you could e.g. overload
operator new for the class and do some hacks there to avoid creation
on the stack at runtime. But I don't know a good reason why one would
want to do that. Quite the contrary, objects on the stack are a C++
asset compared to other popular languages. (BTW, Java is said to have
objects on the stack 'behind the scenes' in one of the next versions.)

Best regards,
Roland Pibinger
 
K

kwikius

Probably not for a compile time solution but you could e.g. overload
operator new for the class and do some hacks there to avoid creation
on the stack at runtime. But I don't know a good reason why one would
want to do that.

One reason is that the semantics of heap allocated objects are
different in an important way. You are in control of the lifetime of
the object. Hence smart pointers such as shared_ptr.( Though you can
violate the guarantee with shared_ptr using a 'null deleter'.) With
stack allocated objects you are not. In fact new could be looked at as
a modifier to the type of an object, like const.

Also note that the straight new operator does two things with
orthogonal purpose. One it allocates memory, Two it constructs an
object.

The purpose behind the allocation can be either that you want the
object to be more or less permanent, outside control of the stack, or
because you need an object whose size cannot be known until runtime or
both.

One downside of acquiring a chunk of memory at runtime is that it can
be a slow business. The other is that the compiler finds less
opportunity for optimisation.

All these things add up to important differences between 'auto' and
heap allocated objects.


regards
Andy Little
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top