Prevent a class instance being created on the stack. Possible somehow?

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.
 
F

FabioAng

Joerg Toellner said:
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

See More Effective C++ - Item 27: Requiring or prohibiting heap-based
objects.
You can write something like this:

class MyClass {
public:
MyClass();
private:
void destroy() const { delete this; }

...

private:
~MyClass();
};

int main(int argc,char** argv)
{
MyClass myclass; // <--- error, privete destructor called here !!!

MyClass* myclass_ptr = new MyClass;
myclass_ptr->destroy();
}
 
J

Joerg Toellner

Hi Fabio,

many thanks for pointing me in the right direction. Shame on me...i own this
great books (EC++ and MEC++) but haven't thougt of them that they standing
on my bookshelf.

In germany we say sometimes "You don't see the wood, because of all the many
trees". And that was such a situation. :-/

Thanks again
CU
Joerg
 
M

mlimber

FabioAng said:
See More Effective C++ - Item 27: Requiring or prohibiting heap-based
objects.
You can write something like this:

class MyClass {
public:
MyClass();
private:
void destroy() const { delete this; }

...

private:
~MyClass();
};

int main(int argc,char** argv)
{
MyClass myclass; // <--- error, privete destructor called here !!!

MyClass* myclass_ptr = new MyClass;
myclass_ptr->destroy();
}

See also the FAQ for caveats on this technique:

http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.15

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top