 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Kirit Sælensminde Guest
|
Posted: Sat Jun 17, 2006 9:10 am Post subject: Preventing stack instances |
|
|
I know that making new protected or private will (generally) prevent
instances from being created on the heap, but I was wondering about
preventing them on the stack.
I saw in another post a hint about protecting the destructor. As the
objects in question are all managed through a single smart pointer type
I suspect that something like the following should work:
class MyObjectPtr;
class MyObject { // There are manu sub-classes of this type
protected:
virtual ~MyObject();
friend MyObjectPtr;
};
class MyObjectPtr {
public:
~MyObjectPtr() {
delete object;
}
private:
MyObject *object;
};
[Example is really pseudo-code with anything not relevant just skipped]
Is this sufficient? Are there any 'gotchas' associated with this?
TIA
Kirit |
|
| Back to top |
|
 |
Kirit Sælensminde Guest
|
Posted: Sat Jun 17, 2006 9:10 am Post subject: Re: Preventing stack instances |
|
|
Alf P. Steinbach wrote:
| Quote: | Are there any 'gotchas' associated with this?
Yes. A restriction to dynamic allocation is not very meaningful unless
it's exploited some way, which typically means that the objects will
participate in some linked data structure. When you restrict a class to
dynamic allocation you should therefore in general also take charge of
(or prevent) copying.
|
Of course. Thanks. The class already derives from boost::noncopyable so
that's covered.
Roland Pibinger wrote:
| Quote: | you can create derived objects on the stack, e.g.
class MyObjectEx : public MyObject {};
|
Thanks. It means that the person doing it is specifically circumventing
the protection though. As such I guess they will get everything they
deserve :-)
| Quote: | In contrast to Java, which is a heap-oriented language, C++ works best
as stack-oriented language. Even in your example the heap allocated
objects are managed by a stack-based "smart" pointer. You should avoid
heap objects as much as possible. They produce management and
performance overhead.
|
C++ is not heap oriented? Ummm, Ok. That seems a peculiar view to take.
Of course on the matter of performance you cannot say, in general, that
stack or heap will be faster without measuring each. For some classes
heap will be faster, for others it will be faster on the stack. It all
depends on what the class is doing; how it is managing whatever
ancilliary memory/objects it controls; its own access pattern and its
create/destroy pattern.
Of course the 'management overhead' is exactly what I was asking about.
K |
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Sat Jun 17, 2006 9:10 am Post subject: Re: Preventing stack instances |
|
|
* Kirit Sælensminde:
| Quote: | I know that making new protected or private will (generally) prevent
instances from being created on the heap, but I was wondering about
preventing them on the stack.
I saw in another post a hint about protecting the destructor. As the
objects in question are all managed through a single smart pointer type
I suspect that something like the following should work:
class MyObjectPtr;
class MyObject { // There are manu sub-classes of this type
protected:
virtual ~MyObject();
friend MyObjectPtr;
};
class MyObjectPtr {
public:
~MyObjectPtr() {
delete object;
}
private:
MyObject *object;
};
[Example is really pseudo-code with anything not relevant just skipped]
Is this sufficient?
|
For the purpose of ensuring dynamic allocation, yes.
| Quote: | Are there any 'gotchas' associated with this?
|
Yes. A restriction to dynamic allocation is not very meaningful unless
it's exploited some way, which typically means that the objects will
participate in some linked data structure. When you restrict a class to
dynamic allocation you should therefore in general also take charge of
(or prevent) copying.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? |
|
| Back to top |
|
 |
Roland Pibinger Guest
|
Posted: Sat Jun 17, 2006 9:10 am Post subject: Re: Preventing stack instances |
|
|
On 16 Jun 2006 22:46:35 -0700, "=?iso-8859-1?q?Kirit_S=E6lensminde?="
<kirit.saelensminde (AT) gmail (DOT) com> wrote:
| Quote: | I know that making new protected or private will (generally) prevent
instances from being created on the heap, but I was wondering about
preventing them on the stack.
I saw in another post a hint about protecting the destructor.
|
.... and the constructor(s)
| Quote: | As the
objects in question are all managed through a single smart pointer type
I suspect that something like the following should work:
class MyObjectPtr;
class MyObject { // There are manu sub-classes of this type
protected:
virtual ~MyObject();
friend MyObjectPtr;
};
|
you can create derived objects on the stack, e.g.
class MyObjectEx : public MyObject {};
| Quote: | class MyObjectPtr {
public:
~MyObjectPtr() {
delete object;
}
private:
MyObject *object;
};
[Example is really pseudo-code with anything not relevant just skipped]
Is this sufficient? Are there any 'gotchas' associated with this?
|
In contrast to Java, which is a heap-oriented language, C++ works best
as stack-oriented language. Even in your example the heap allocated
objects are managed by a stack-based "smart" pointer. You should avoid
heap objects as much as possible. They produce management and
performance overhead.
Best wishes,
Roland Pibinger |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|