J
Joerg Toellner
Dear Group,
i have a self written c++-class that is used in many projects from many
programmers.
The functionality of this class requests, for some reasons, that any
instance (object) of this special class MUST be created in the heap with the
new-operator. Any instance created locally on the stack should be considered
"illegal".
Is there any possibility in class design with c++ or a precompiler directive
(i am using MSVC .NET Enterprise Architekt 7.1)or whatsoever, that prevents
creation on the stack? If it's done on the stack, it would be great if the
compiler, while compile time, could issue an error or warning that informs
the programmer, that he must not do that.
If this is not possible, is there a way to recognize while runtime if the
actual instance is on the stack or heap, so i can put some code in the
constructor and i.e.issue a kind of dialogbox in debugmode that shows the
tester that there will be going something horribly wrong?
Of course i can (and have done) document this behaviour in my class
documentation and/or sourcecode, but as you surely know "NOBODY reads
documentations - All of us only write them!" )) So i would really prefer
to hit them a "virtual boxing glove" right onto the tip of their noses if
they do such "bad things".
Hoping somebody has a trick/hint/url/article/sample how to achieve this
behavior.
TIA very much
Joerg
PS:
If you are interested why i need this, the class is designed to start
external programs (third party programs) and communicate with them over
pipes (stdin/out/err). It is possible with this class, that your main
program can end (or at least the actual function - mostly an event handler)
and the class detaches itself from the main program and ends (and deletes)
itself when the started external child process ends. But if the instance is
on the stack, the object dies when your function/program ends and this can
be too early (before the child ends) and so some messages will go into
nothing and this results in a crash. This should not happen.
i have a self written c++-class that is used in many projects from many
programmers.
The functionality of this class requests, for some reasons, that any
instance (object) of this special class MUST be created in the heap with the
new-operator. Any instance created locally on the stack should be considered
"illegal".
Is there any possibility in class design with c++ or a precompiler directive
(i am using MSVC .NET Enterprise Architekt 7.1)or whatsoever, that prevents
creation on the stack? If it's done on the stack, it would be great if the
compiler, while compile time, could issue an error or warning that informs
the programmer, that he must not do that.
If this is not possible, is there a way to recognize while runtime if the
actual instance is on the stack or heap, so i can put some code in the
constructor and i.e.issue a kind of dialogbox in debugmode that shows the
tester that there will be going something horribly wrong?
Of course i can (and have done) document this behaviour in my class
documentation and/or sourcecode, but as you surely know "NOBODY reads
documentations - All of us only write them!" )) So i would really prefer
to hit them a "virtual boxing glove" right onto the tip of their noses if
they do such "bad things".
Hoping somebody has a trick/hint/url/article/sample how to achieve this
behavior.
TIA very much
Joerg
PS:
If you are interested why i need this, the class is designed to start
external programs (third party programs) and communicate with them over
pipes (stdin/out/err). It is possible with this class, that your main
program can end (or at least the actual function - mostly an event handler)
and the class detaches itself from the main program and ends (and deletes)
itself when the started external child process ends. But if the instance is
on the stack, the object dies when your function/program ends and this can
be too early (before the child ends) and so some messages will go into
nothing and this results in a crash. This should not happen.