Gmail ͏°´õ ¹®¼­µµ±¸ ¸®´õ »çÁø »çÀÌÆ® µµ±¸ Google °Ë»ö YouTube ´õº¸±â »
µµ¿ò¸» | ·Î±×ÀÎ
Google ±×·ì Ȩ
Forcing derived classes to be on the heap
ÇöÀç ÀÌ ±×·ì¿¡ ¿ì¼± Ç¥½ÃµÇ´Â ÁÖÁ¦°¡ ³Ê¹« ¸¹½À´Ï´Ù. ÀÌ ÁÖÁ¦°¡ ¿ì¼± Ç¥½ÃµÇµµ·Ï ÇÏ·Á¸é ´Ù¸¥ ÁÖÁ¦¿¡¼­ ¿ì¼±Ç¥½Ã ¿É¼ÇÀ» »èÁ¦Çϼ¼¿ä.
¿äûÀ» ó¸®ÇÏ´Â µµÁß ¿À·ù°¡ ¹ß»ýÇß½À´Ï´Ù. ´Ù½Ã ½ÃµµÇØ ÁֽʽÿÀ.
Ç÷¡±×
  ¸Þ½ÃÁö 8°³ - ¸ðµÎ ÆîÄ¡±â  -  ¸ðµÎ ·Î ¹ø¿ª ¹ø¿ªµÊ(Àüü ¿ø¹® º¸±â)
¸Þ½ÃÁö¸¦ °Ô½ÃÇÏ½Ç ±×·ìÀº À¯Áî³Ý ±×·ìÀÔ´Ï´Ù. ÀÌ ±×·ì¿¡ ¸Þ½ÃÁö¸¦ °Ô½ÃÇϽøé ÀÎÅͳÝÀÇ ¸ðµç »ç¿ëÀÚ°¡ »ç¿ëÀÚÀÇ À̸ÞÀÏÀ» º¼ ¼öµµ ÀÖ½À´Ï´Ù.
´ä±ÛÀÌ Àü¼ÛµÇÁö ¾Ê¾Ò½À´Ï´Ù.
°Ô½ÃµÇ¾ú½À´Ï´Ù.
 
º¸³½»ç¶÷:
¹Þ´Â»ç¶÷:
ÂüÁ¶:
Ãß°¡ ´äº¯:
ÂüÁ¶ Ãß°¡ | Ãß°¡ ´äº¯ Ãß°¡ | Á¦¸ñ ¼öÁ¤
Á¦¸ñ:
È®ÀÎ:
È®ÀÎÀ» À§ÇØ ¾Æ·¡ ±×¸²¿¡ Ç¥½ÃµÈ ¹®ÀÚ¸¦ ÀÔ·ÂÇϰųª ¾×¼¼½º ¾ÆÀÌÄÜÀ» Ŭ¸¯ÇÏ¿© µé¸®´Â ¼ýÀÚ¸¦ ÀÔ·ÂÇϼ¼¿ä. ¼ýÀÚ¸¦ µéÀº ÈÄ ÇØ´ç ¼ýÀÚ¸¦ ÀÔ·ÂÇϼ¼¿ä.
 
Ury Jamshy  
ÇÁ·ÎÇÊ º¸±â   ·Î ¹ø¿ª ¹ø¿ªµÊ(¿ø¹® º¸±â)
 Ãß°¡ ¿É¼Ç 2002³â3¿ù26ÀÏ, ¿ÀÀü3½Ã30ºÐ
´º½º±×·ì: comp.lang.c++.moderated
º¸³½»ç¶÷: "Ury Jamshy" <ujam...@netvision.net.il>
³¯Â¥: 25 Mar 2002 13:31:05 -0500
ÇöÁö½Ã°£: 2002³â3¿ù26ÀÏ(È­) ¿ÀÀü3½Ã31ºÐ
Á¦¸ñ: Forcing derived classes to be on the heap
How can I force classes derived from a specific base class to be allocated
on the heap ?
If I write both Base and Berived then there is a standard solution:
Define Derived destructur as private and provide a Destroy method:

class Base  // abstract class
{
public:
     virtual ~Base();
     void Destroy() { delete this; }

};

class Derived: public base
{
protected:
     ~Derived(); //User must allocate on the heap and destroy using
Base::Destroy

};

But that does not prevent anybody from deriving from Base and declaring the
destructor as public, thus causing unpleasant behavior when Destroy is
called.

Ury.

      [ Send an empty e-mail to c++-h...@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]


    Àü´Þ  
¸Þ½ÃÁö¸¦ °Ô½ÃÇÏ·Á¸é ±×·ì¿¡ °¡ÀÔÇØ¾ß ÇÕ´Ï´Ù.
¸Þ½ÃÁö¸¦ °Ô½ÃÇÏ·Á¸é ¸ÕÀú ÇØ´ç ±×·ì¿¡ °¡ÀÔÇÏ¼Å¾ß ÇÕ´Ï´Ù.
°Ô½ÃÇϱâ Àü¿¡ °¡ÀÔ ¼³Á¤ ÆäÀÌÁö¿¡¼­ ´Ð³×ÀÓÀ» ¾÷µ¥ÀÌÆ®Çϼ¼¿ä.
¸Þ½ÃÁö¸¦ °Ô½ÃÇÒ ¼ö ÀÖ´Â ±ÇÇÑÀÌ ¾ø½À´Ï´Ù.
Peter Simons  
ÇÁ·ÎÇÊ º¸±â   ·Î ¹ø¿ª ¹ø¿ªµÊ(¿ø¹® º¸±â)
 Ãß°¡ ¿É¼Ç 2002³â3¿ù26ÀÏ, ¿ÀÀü8½Ã19ºÐ
´º½º±×·ì: comp.lang.c++.moderated
º¸³½»ç¶÷: Peter Simons <sim...@cryp.to>
³¯Â¥: 25 Mar 2002 18:20:07 -0500
ÇöÁö½Ã°£: 2002³â3¿ù26ÀÏ(È­) ¿ÀÀü8½Ã20ºÐ
Á¦¸ñ: Re: Forcing derived classes to be on the heap
See section 16.20 of the C++ FAQ. You'll find that at:

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

      [ Send an empty e-mail to c++-h...@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]


    Àü´Þ  
¸Þ½ÃÁö¸¦ °Ô½ÃÇÏ·Á¸é ±×·ì¿¡ °¡ÀÔÇØ¾ß ÇÕ´Ï´Ù.
¸Þ½ÃÁö¸¦ °Ô½ÃÇÏ·Á¸é ¸ÕÀú ÇØ´ç ±×·ì¿¡ °¡ÀÔÇÏ¼Å¾ß ÇÕ´Ï´Ù.
°Ô½ÃÇϱâ Àü¿¡ °¡ÀÔ ¼³Á¤ ÆäÀÌÁö¿¡¼­ ´Ð³×ÀÓÀ» ¾÷µ¥ÀÌÆ®Çϼ¼¿ä.
¸Þ½ÃÁö¸¦ °Ô½ÃÇÒ ¼ö ÀÖ´Â ±ÇÇÑÀÌ ¾ø½À´Ï´Ù.
Kevin Cline  
ÇÁ·ÎÇÊ º¸±â   ·Î ¹ø¿ª ¹ø¿ªµÊ(¿ø¹® º¸±â)
 Ãß°¡ ¿É¼Ç 2002³â3¿ù26ÀÏ, ¿ÀÈÄ4½Ã22ºÐ
´º½º±×·ì: comp.lang.c++.moderated
º¸³½»ç¶÷: kcl...@optelnow.net (Kevin Cline)
³¯Â¥: 26 Mar 2002 02:22:54 -0500
ÇöÁö½Ã°£: 2002³â3¿ù26ÀÏ(È­) ¿ÀÈÄ4½Ã22ºÐ
Á¦¸ñ: Re: Forcing derived classes to be on the heap
"Ury Jamshy" <ujam...@netvision.net.il> wrote in message

<news:a7na34$e6q$1@news.netvision.net.il>...

> How can I force classes derived from a specific base class to be allocated
> on the heap ?

Make the base class destructor protected and virtual.

class Base  // abstract class
{
protected:
  virtual ~Base();
public:
  void Destroy() { delete this; }

};

However, I'm very suspicious of this code pattern.
Every time I've seen it there was a better
solution that didn't involve self-deleting objects,
and thus eliminated the restriction that instances of some
class had to be on the heap.

      [ Send an empty e-mail to c++-h...@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]


    Àü´Þ  
¸Þ½ÃÁö¸¦ °Ô½ÃÇÏ·Á¸é ±×·ì¿¡ °¡ÀÔÇØ¾ß ÇÕ´Ï´Ù.
¸Þ½ÃÁö¸¦ °Ô½ÃÇÏ·Á¸é ¸ÕÀú ÇØ´ç ±×·ì¿¡ °¡ÀÔÇÏ¼Å¾ß ÇÕ´Ï´Ù.
°Ô½ÃÇϱâ Àü¿¡ °¡ÀÔ ¼³Á¤ ÆäÀÌÁö¿¡¼­ ´Ð³×ÀÓÀ» ¾÷µ¥ÀÌÆ®Çϼ¼¿ä.
¸Þ½ÃÁö¸¦ °Ô½ÃÇÒ ¼ö ÀÖ´Â ±ÇÇÑÀÌ ¾ø½À´Ï´Ù.
Francis Glassborow  
ÇÁ·ÎÇÊ º¸±â   ·Î ¹ø¿ª ¹ø¿ªµÊ(¿ø¹® º¸±â)
 Ãß°¡ ¿É¼Ç 2002³â3¿ù27ÀÏ, ¿ÀÀü1½Ã34ºÐ
´º½º±×·ì: comp.lang.c++.moderated
º¸³½»ç¶÷: Francis Glassborow <francis.glassbo...@ntlworld.com>
³¯Â¥: 26 Mar 2002 11:35:33 -0500
ÇöÁö½Ã°£: 2002³â3¿ù27ÀÏ(¼ö) ¿ÀÀü1½Ã35ºÐ
Á¦¸ñ: Re: Forcing derived classes to be on the heap
In article <x7ofhcbaeq....@janus.cryp.to>, Peter Simons <sim...@cryp.to>
writes

>See section 16.20 of the C++ FAQ. You'll find that at:

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

Yes, and how does that solve the OP's problem. He wants a base class all
of whose derived variants must also be on the heap. Maybe I am being
dense but I do not see how that solves this problem. IIRC protected
dtors are accessible to derived classes.

--
Francis Glassborow
Check out the ACCU Spring Conference 2002
4 Days, 4 tracks, 4+ languages, World class speakers
For details see: http://www.accu.org/events/public/accu0204.htm

      [ Send an empty e-mail to c++-h...@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]


    Àü´Þ  
¸Þ½ÃÁö¸¦ °Ô½ÃÇÏ·Á¸é ±×·ì¿¡ °¡ÀÔÇØ¾ß ÇÕ´Ï´Ù.
¸Þ½ÃÁö¸¦ °Ô½ÃÇÏ·Á¸é ¸ÕÀú ÇØ´ç ±×·ì¿¡ °¡ÀÔÇÏ¼Å¾ß ÇÕ´Ï´Ù.
°Ô½ÃÇϱâ Àü¿¡ °¡ÀÔ ¼³Á¤ ÆäÀÌÁö¿¡¼­ ´Ð³×ÀÓÀ» ¾÷µ¥ÀÌÆ®Çϼ¼¿ä.
¸Þ½ÃÁö¸¦ °Ô½ÃÇÒ ¼ö ÀÖ´Â ±ÇÇÑÀÌ ¾ø½À´Ï´Ù.
Peter Simons  
ÇÁ·ÎÇÊ º¸±â   ·Î ¹ø¿ª ¹ø¿ªµÊ(¿ø¹® º¸±â)
 Ãß°¡ ¿É¼Ç 2002³â3¿ù27ÀÏ, ¿ÀÀü7½Ã09ºÐ
´º½º±×·ì: comp.lang.c++.moderated
º¸³½»ç¶÷: Peter Simons <sim...@cryp.to>
³¯Â¥: 26 Mar 2002 17:10:17 -0500
ÇöÁö½Ã°£: 2002³â3¿ù27ÀÏ(¼ö) ¿ÀÀü7½Ã10ºÐ
Á¦¸ñ: Re: Forcing derived classes to be on the heap

Francis Glassborow writes:

  > [...]

Apparently you misunderstood the text: The FAQ does not suggest that
the destructor is made protected but that the _constructor_ be
private.

      [ Send an empty e-mail to c++-h...@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]


    Àü´Þ  
¸Þ½ÃÁö¸¦ °Ô½ÃÇÏ·Á¸é ±×·ì¿¡ °¡ÀÔÇØ¾ß ÇÕ´Ï´Ù.
¸Þ½ÃÁö¸¦ °Ô½ÃÇÏ·Á¸é ¸ÕÀú ÇØ´ç ±×·ì¿¡ °¡ÀÔÇÏ¼Å¾ß ÇÕ´Ï´Ù.
°Ô½ÃÇϱâ Àü¿¡ °¡ÀÔ ¼³Á¤ ÆäÀÌÁö¿¡¼­ ´Ð³×ÀÓÀ» ¾÷µ¥ÀÌÆ®Çϼ¼¿ä.
¸Þ½ÃÁö¸¦ °Ô½ÃÇÒ ¼ö ÀÖ´Â ±ÇÇÑÀÌ ¾ø½À´Ï´Ù.
Kevin Cline  
ÇÁ·ÎÇÊ º¸±â   ·Î ¹ø¿ª ¹ø¿ªµÊ(¿ø¹® º¸±â)
 Ãß°¡ ¿É¼Ç 2002³â3¿ù27ÀÏ, ¿ÀÀü8½Ã22ºÐ
´º½º±×·ì: comp.lang.c++.moderated
º¸³½»ç¶÷: kcl...@optelnow.net (Kevin Cline)
³¯Â¥: 26 Mar 2002 18:22:40 -0500
ÇöÁö½Ã°£: 2002³â3¿ù27ÀÏ(¼ö) ¿ÀÀü8½Ã22ºÐ
Á¦¸ñ: Re: Forcing derived classes to be on the heap

Francis Glassborow <francis.glassbo...@ntlworld.com> wrote in message <news:pG+PGDEV7Eo8EwsL@robinton.ntlworld.com>...
> In article <x7ofhcbaeq....@janus.cryp.to>, Peter Simons <sim...@cryp.to>
> writes
> >See section 16.20 of the C++ FAQ. You'll find that at:

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

> Yes, and how does that solve the OP's problem. He wants a base class all
> of whose derived variants must also be on the heap. Maybe I am being
> dense but I do not see how that solves this problem. IIRC protected
> dtors are accessible to derived classes.

I posted the same solution as given in the FAQ and Francis is
correct; it does not solve the OP's problem.  I don't
believe that his problem can be solved -- if the base class allows
public derivation then it will be possible to write a derived class with a
public constructor.  I would advise the OP to re-examine his need
for such a class.

      [ Send an empty e-mail to c++-h...@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]


    Àü´Þ  
¸Þ½ÃÁö¸¦ °Ô½ÃÇÏ·Á¸é ±×·ì¿¡ °¡ÀÔÇØ¾ß ÇÕ´Ï´Ù.
¸Þ½ÃÁö¸¦ °Ô½ÃÇÏ·Á¸é ¸ÕÀú ÇØ´ç ±×·ì¿¡ °¡ÀÔÇÏ¼Å¾ß ÇÕ´Ï´Ù.
°Ô½ÃÇϱâ Àü¿¡ °¡ÀÔ ¼³Á¤ ÆäÀÌÁö¿¡¼­ ´Ð³×ÀÓÀ» ¾÷µ¥ÀÌÆ®Çϼ¼¿ä.
¸Þ½ÃÁö¸¦ °Ô½ÃÇÒ ¼ö ÀÖ´Â ±ÇÇÑÀÌ ¾ø½À´Ï´Ù.
Francis Glassborow  
ÇÁ·ÎÇÊ º¸±â   ·Î ¹ø¿ª ¹ø¿ªµÊ(¿ø¹® º¸±â)
 Ãß°¡ ¿É¼Ç 2002³â3¿ù29ÀÏ, ¿ÀÀü1½Ã48ºÐ
´º½º±×·ì: comp.lang.c++.moderated
º¸³½»ç¶÷: Francis Glassborow <francis.glassbo...@ntlworld.com>
³¯Â¥: 28 Mar 2002 11:49:07 -0500
ÇöÁö½Ã°£: 2002³â3¿ù29ÀÏ(±Ý) ¿ÀÀü1½Ã49ºÐ
Á¦¸ñ: Re: Forcing derived classes to be on the heap
In article <x7adsv44tl....@janus.cryp.to>, Peter Simons <sim...@cryp.to>
writes

>Francis Glassborow writes:

>  > [...]

>Apparently you misunderstood the text: The FAQ does not suggest that
>the destructor is made protected but that the _constructor_ be
>private.

Sure, but that makes no difference (other than you better make sure you
have remembered to declare a private copy ctor as well.) Could we focus
on ideas that might solve his problem which is not the one in the FAQ.
He wants to force all derived classes off the stack. I would be
interested in an answer because I do not know of one. A protected ctor
is AFAIR, accessible to derived class ctors, and private ones simply
prevent direct construction of both the base and derived objects.

--
Francis Glassborow
Check out the ACCU Spring Conference 2002
4 Days, 4 tracks, 4+ languages, World class speakers
For details see: http://www.accu.org/events/public/accu0204.htm

      [ Send an empty e-mail to c++-h...@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]


    Àü´Þ  
¸Þ½ÃÁö¸¦ °Ô½ÃÇÏ·Á¸é ±×·ì¿¡ °¡ÀÔÇØ¾ß ÇÕ´Ï´Ù.
¸Þ½ÃÁö¸¦ °Ô½ÃÇÏ·Á¸é ¸ÕÀú ÇØ´ç ±×·ì¿¡ °¡ÀÔÇÏ¼Å¾ß ÇÕ´Ï´Ù.
°Ô½ÃÇϱâ Àü¿¡ °¡ÀÔ ¼³Á¤ ÆäÀÌÁö¿¡¼­ ´Ð³×ÀÓÀ» ¾÷µ¥ÀÌÆ®Çϼ¼¿ä.
¸Þ½ÃÁö¸¦ °Ô½ÃÇÒ ¼ö ÀÖ´Â ±ÇÇÑÀÌ ¾ø½À´Ï´Ù.
Ruslan Abdikeev  
ÇÁ·ÎÇÊ º¸±â   ·Î ¹ø¿ª ¹ø¿ªµÊ(¿ø¹® º¸±â)
 Ãß°¡ ¿É¼Ç 2002³â3¿ù29ÀÏ, ¿ÀÀü5½Ã01ºÐ
´º½º±×·ì: comp.lang.c++.moderated
º¸³½»ç¶÷: "Ruslan Abdikeev" <ruslan_abdikeevREMOVE...@hotmail.com>
³¯Â¥: 28 Mar 2002 15:02:43 -0500
ÇöÁö½Ã°£: 2002³â3¿ù29ÀÏ(±Ý) ¿ÀÀü5½Ã02ºÐ
Á¦¸ñ: Re: Forcing derived classes to be on the heap
"Francis Glassborow" <francis.glassbo...@ntlworld.com> wrote in message

news:Bj+VP3AWeQo8EwLv@robinton.ntlworld.com...
 > In article <x7adsv44tl....@janus.cryp.to>, Peter Simons <sim...@cryp.to>
 > writes
 > Sure, but that makes no difference (other than you better make sure you
 > have remembered to declare a private copy ctor as well.) Could we focus
 > on ideas that might solve his problem which is not the one in the FAQ.
 > He wants to force all derived classes off the stack. I would be
 > interested in an answer because I do not know of one. A protected ctor
 > is AFAIR, accessible to derived class ctors, and private ones simply
 > prevent direct construction of both the base and derived objects.
 >

Just a quick thought.
The best way to do a compile-time enforcement is to prohibit
working with objects directly, and use some kind of smart pointers,
that will accept/work only with objects with dynamic storage
duration.

If a runtime enforcement would be acceptable and it was acceptable
to prohibit derived classes to overload operator new, there is a simple
and obvious solution (below).
It's somewhat fragile to my sense, but it works.

However, I suspect that there couldn't be a C++ standard conforming
compile time enforcement, just because there is explicitly defined
"stack space" in C++ standard (as opposed to free store).
(I hardly believe we could use "stack unwinding" for these purposes.)

So the OP's question is about differentiating objects with automatic
or static storage duration and objects with dynamic storage duration
at compile time.

AFAIK, the only relevant difference is the way objects with
dynamic storage duration get created/destroyed  (3.7.3/1),
but I don't see how one can exploit it at compile time.

So I would be interested in an answer as well,
because I couldn't think of one.

A runtime enforcement helper sample:

   #include <cassert>
   #include <cstddef>
   struct rt_heapalloc_check
   {
     virtual ~rt_heapalloc_check() {}
     void* operator new(std::size_t sz) { ++heap_check; return ::operator new(sz); }
     void operator delete(void* p) { ::operator delete(p); }
   protected:
     rt_heapalloc_check() { assert(--heap_check==0); }
   private:
     static int heap_check;
   };
   int rt_heapalloc_check::heap_check = 0;

   struct foo : rt_heapalloc_check {};

   int main()
   {
     //B b; //at runtime this will fail an assertion
     B* b= new B; //works fine
     return 0;
   }

Sincerely,
Ruslan Abdikeev

      [ Send an empty e-mail to c++-h...@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]


    Àü´Þ  
¸Þ½ÃÁö¸¦ °Ô½ÃÇÏ·Á¸é ±×·ì¿¡ °¡ÀÔÇØ¾ß ÇÕ´Ï´Ù.
¸Þ½ÃÁö¸¦ °Ô½ÃÇÏ·Á¸é ¸ÕÀú ÇØ´ç ±×·ì¿¡ °¡ÀÔÇÏ¼Å¾ß ÇÕ´Ï´Ù.
°Ô½ÃÇϱâ Àü¿¡ °¡ÀÔ ¼³Á¤ ÆäÀÌÁö¿¡¼­ ´Ð³×ÀÓÀ» ¾÷µ¥ÀÌÆ®Çϼ¼¿ä.
¸Þ½ÃÁö¸¦ °Ô½ÃÇÒ ¼ö ÀÖ´Â ±ÇÇÑÀÌ ¾ø½À´Ï´Ù.
¸Þ½ÃÁöÀÇ ³¡
« Åä·ÐÀ¸·Î µ¹¾Æ°¡±â « ´ÙÀ½     ÀÌÀü »

±×·ì ¸¸µé±â - Google ±×·ì½º - Google Ȩ - ¼­ºñ½º ¾à°ü - °³ÀÎÁ¤º¸ º¸È£Á¤Ã¥
©2010 Google