Heap-only objects
Hi
is there some way to make classes in c++ impossible to create on the stack but not on the heap?
Like this:
class Foo{};
Foo f; //make impossible
int main()
{
Foo* pf = new Foo; //still be possible
return 0;
}
Grateful for answers
Making the constructor private, I believe, will do the trick. [edit]And then making a static member function that creates the object.[/edit]
class CHeapOnly{ public: static CHeapOnly* Create() { return new CHeapOnly; } private: CHeapOnly() {}};int main(){ CHeapOnly* pHeapOnly = CHeapOnly::Create(); delete pHeapOnly; return 0;}
It has a number of flaws
operator new could also be overloaded to allocate from a stack based resource.
class CHeapOnly{ public: static CHeapOnly* Create() { return new CHeapOnly; } private: CHeapOnly() {}};int main(){ CHeapOnly* pHeapOnly = CHeapOnly::Create(); CHeapOnly itsonthestack(*pHeapOnly); delete pHeapOnly; return 0;}
operator new could also be overloaded to allocate from a stack based resource.
Ok but is there any other way to do the same thing
And what happens if you derive a class from CHeapOnly? didnt work for me.
And what happens if you derive a class from CHeapOnly? didnt work for me.
Making them protected, though, would allow others to derive from it and ignore the static heap allocating function, and allocate on the stack.
From what I have gathered, compiler protections such as private and protected members, and const variables, etcetera, can always be circumvented if someone really wants to. These features don't exist to force malevolent programmers to do what they're suppose to; they exist to help point out when innocent, good-intentioned programmers make a mistake.
The level of perfection and strength that is needed, I suppose, is based on the intended use. I guess there may be times that you want to make it as almost impossible to do something against the intended rules. But most of the time, it's not that critical. So I guess my question is to the OP: What's this for?
(In fact, after answering the question, I realized that it might simply be for homework, and I shouldn't have just thrown out a solution (perfect or imperfect) without checking first. Oh well.)
The level of perfection and strength that is needed, I suppose, is based on the intended use. I guess there may be times that you want to make it as almost impossible to do something against the intended rules. But most of the time, it's not that critical. So I guess my question is to the OP: What's this for?
(In fact, after answering the question, I realized that it might simply be for homework, and I shouldn't have just thrown out a solution (perfect or imperfect) without checking first. Oh well.)
Quote:Original post by Agony
From what I have gathered, compiler protections such as private and protected members, and const variables, etcetera, can always be circumvented if someone really wants to. These features don't exist to force malevolent programmers to do what they're suppose to; they exist to help point out when innocent, good-intentioned programmers make a mistake.
The level of perfection and strength that is needed, I suppose, is based on the intended use. I guess there may be times that you want to make it as almost impossible to do something against the intended rules. But most of the time, it's not that critical. So I guess my question is to the OP: What's this for?
(In fact, after answering the question, I realized that it might simply be for homework, and I shouldn't have just thrown out a solution (perfect or imperfect) without checking first. Oh well.)
True, which is another reason as to why making it only heap based is impossible.
This is NOT for homework. We dont event not what a heap is in my class, and i doubt thats even part of our cours.
Anyways, im working on a sidescroller lierostyle game. This heap-only class is to be used in my sprite-baseclass so that sprite classes wont be created before the main function. DirectX would in that case not have been loaded properly and the sprites, trying to create dx-surfaces, would cause errors when trying to use directx without a directx-interface.
Maybe(probably) theres another better way solve this problem, like haveing all dx-dependent code in a separate sprite function called in the main-loop instead of haveing it in the constructors. But it is always best to ask...
Feedback plz.
Anyways, im working on a sidescroller lierostyle game. This heap-only class is to be used in my sprite-baseclass so that sprite classes wont be created before the main function. DirectX would in that case not have been loaded properly and the sprites, trying to create dx-surfaces, would cause errors when trying to use directx without a directx-interface.
Maybe(probably) theres another better way solve this problem, like haveing all dx-dependent code in a separate sprite function called in the main-loop instead of haveing it in the constructors. But it is always best to ask...
Feedback plz.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement