Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 23rd, 2005, 06:00 AM
ES Kim
Guest
 
Posts: n/a
Default requiring a heap-based objects

MEC++ Item 17 explains how to require an object be constructed only
on heap. No automatic, no static objects, that is. But I can't
find how to enforce the requirement for derived classes too.

1. make the base destructor private
No class can be derived from the base. Game over.

2. make the base destructor protected
Derived destructors should be also protected to meet the requirement,
but all bets are off once you forget to declare explicit destructor.
(It's the way most of us do most of the time.)

Any suggestion to enforce the requirement?

--

ES Kim


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

  #2  
Old July 23rd, 2005, 06:01 AM
Maxim Yegorushkin
Guest
 
Posts: n/a
Default Re: requiring a heap-based objects

On Fri, 03 Jun 2005 11:55:04 +0400, ES Kim <group.eskim@gmail.com> wrote:
[color=blue]
> MEC++ Item 17 explains how to require an object be constructed only
> on heap. No automatic, no static objects, that is. But I can't[/color]

Why would you want to do that?
[color=blue]
> find how to enforce the requirement for derived classes too.
>
> 1. make the base destructor private
> No class can be derived from the base. Game over.
>
> 2. make the base destructor protected
> Derived destructors should be also protected to meet the requirement,
> but all bets are off once you forget to declare explicit destructor.
> (It's the way most of us do most of the time.)[/color]

Another way is to provide interfaces and factory functions, so that a user
has no way to create an object but to invoke the factory:

struct some
{
virtual ~some() {}
virtual void foo() = 0;
};
std::auto_ptr<some> create_some();

--
Maxim Yegorushkin

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

  #3  
Old July 23rd, 2005, 06:03 AM
Hendrik Schober
Guest
 
Posts: n/a
Default Re: requiring a heap-based objects

ES Kim <group.eskim@gmail.com> wrote:[color=blue]
> MEC++ Item 17 explains how to require an object be constructed only
> on heap. No automatic, no static objects, that is. But I can't
> find how to enforce the requirement for derived classes too.
> [...][/color]

Wouldn't this work

class heap_base {
public:
void destroy() {delete this;}
private:
virtual ~heap_base() {}
};

or am I missing something?

Schobi

--
SpamTrap@gmx.de is never read
I'm Schobi at suespammers dot org

"Coming back to where you started is not the same as never leaving"
Terry Pratchett



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles