473,324 Members | 2,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

problem realated to C++

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
6 1150
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bruce Davis | last post by:
I'm having a problem on windows (both 2000 and XP) with a multi-threaded tkinter gui application. The problem appears to be a deadlock condition when a child thread pops up a Pmw dialog window in...
11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
0
by: Refky Wahib | last post by:
Hi I need Technical Support I finished a Great project using .Net and SQL Server and .Net Mobile Control My Business case is to implement this Program to accept about 1 Million concurrent...
117
by: Peter Olcott | last post by:
www.halting-problem.com
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
4
by: Heinrich Wolf | last post by:
Hi all I have a history.back() problem with FF(2). IE works as expected, while FF does not. The multi frame website setup as a whole with a lot of frame content switching works flawlessly in...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.