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

problem realated to C++

P: n/a
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

Feb 26 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
Feb 26 '07 #2

P: n/a
Michael DOUBEZ wrote:
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
This is also a FAQ:

http://www.parashift.com/c++-faq-lit...html#faq-16.21
Feb 26 '07 #3

P: n/a
On Mon, 26 Feb 2007 21:46:24 GMT, Mark P
<us****@fall2005REMOVE.fastmailCAPS.fmwrote:
>Michael DOUBEZ wrote:
>Vijay a écrit :
>>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

This is also a FAQ:

http://www.parashift.com/c++-faq-lit...html#faq-16.21
Nope, "program should allow a creation of object only using "new"
operator":

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

Feb 26 '07 #4

P: n/a
Roland Pibinger wrote:
On Mon, 26 Feb 2007 21:46:24 GMT, Mark P
<us****@fall2005REMOVE.fastmailCAPS.fmwrote:
>Michael DOUBEZ wrote:
>>Vijay a écrit :
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
This is also a FAQ:

http://www.parashift.com/c++-faq-lit...html#faq-16.21

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?
Feb 26 '07 #5

P: n/a
On Mon, 26 Feb 2007 23:46:45 GMT, Mark P wrote:
>Roland Pibinger wrote:
>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?
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
Feb 27 '07 #6

P: n/a
On 27 Feb, 09:44, rpbg...@yahoo.com (Roland Pibinger) wrote:
On Mon, 26 Feb 2007 23:46:45 GMT, Mark P wrote:
Roland Pibinger wrote:
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?

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
Feb 27 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.