?
=?iso-8859-1?q?Kirit_S=E6lensminde?=
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
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