Connecting Tech Pros Worldwide Help | Site Map

Derive or not to

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 10:18 AM
bobsled
Guest
 
Posts: n/a
Default Derive or not to

Generally, what should be done to make a class not expected to be derived
from?

What should be done to a class to make it derivable?

In an abstract base class, the destructor is automatically "virtual?

If the constructor of a class is protected or private, how to code the class
so that it provides a public member function, or to declare a friend that
has access to the protected or private constructor and thus the class
becomes instantiable?

Thanks for your comments!



  #2  
Old July 22nd, 2005, 10:18 AM
Sharad Kala
Guest
 
Posts: n/a
Default Re: Derive or not to


"bobsled" <sleding@sands.com> wrote in message
news:wzIjc.30233$_o3.999376@bgtnsc05-news.ops.worldnet.att.net...[color=blue]
> Generally, what should be done to make a class not expected to be derived
> from?[/color]

There is no final keyword like Java to stop derivation in C++.
There is a way though. Read this -
http://www.research.att.com/~bs/bs_f...#no-derivation
[color=blue]
> What should be done to a class to make it derivable?[/color]

By default it is derivable.
[color=blue]
> In an abstract base class, the destructor is automatically "virtual?[/color]

No
[color=blue]
> If the constructor of a class is protected or private, how to code the class
> so that it provides a public member function, or to declare a friend that
> has access to the protected or private constructor and thus the class
> becomes instantiable?[/color]

You could do either way, depends on what you are trying to achieve. The public
member function of the class has to be static though.

-Sharad


  #3  
Old July 22nd, 2005, 10:18 AM
tom_usenet
Guest
 
Posts: n/a
Default Re: Derive or not to

On Wed, 28 Apr 2004 06:55:24 GMT, "bobsled" <sleding@sands.com> wrote:
[color=blue]
>Generally, what should be done to make a class not expected to be derived
>from?[/color]

Don't give it any virtual functions and document the fact that it is a
concrete class. It is possible to force non-derivation, but there
generally isn't much point.
[color=blue]
>What should be done to a class to make it derivable?[/color]

Give it at least one virtual function, and document how that should be
overridden. You will almost always want a virtual destructor too.
[color=blue]
>In an abstract base class, the destructor is automatically "virtual?[/color]

No, you have to explicitly declare it to be virtual.
[color=blue]
>If the constructor of a class is protected or private, how to code the class
>so that it provides a public member function, or to declare a friend that
>has access to the protected or private constructor and thus the class
>becomes instantiable?[/color]

friend class MyFriend;

or

public:
static Foo* createFoo() {return new Foo();}

unless I didn't understand the question?

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

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 220,840 network members.