469,913 Members | 2,683 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,913 developers. It's quick & easy.

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

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.
Sep 26 '05 #1
3 5898

"Joerg Toellner" <to******@oss-gmbh.de> wrote in message
news:dh*************@news.t-online.com...
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();
}
Sep 26 '05 #2
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
"FabioAng" <fa************@libero.it> schrieb im Newsbeitrag
news:dh**********@nnrp-beta.newsland.it...

"Joerg Toellner" <to******@oss-gmbh.de> wrote in message
news:dh*************@news.t-online.com...
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();
}

Sep 26 '05 #3
FabioAng wrote:
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-lit...html#faq-16.15

Cheers! --M

Sep 26 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Felix Wiemann | last post: by
7 posts views Thread by S. Lorétan | last post: by
2 posts views Thread by TEK | last post: by
6 posts views Thread by jimbo | last post: by
19 posts views Thread by jan.loucka | last post: by
20 posts views Thread by tshad | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.