Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Derive or not to

Reply
Thread Tools

Derive or not to

 
 
bobsled
Guest
Posts: n/a
 
      04-28-2004
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!


 
Reply With Quote
 
 
 
 
Sharad Kala
Guest
Posts: n/a
 
      04-28-2004

"bobsled" <(E-Mail Removed)> wrote in message
news:wzIjc.30233$(E-Mail Removed)...
> Generally, what should be done to make a class not expected to be derived
> from?


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

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


By default it is derivable.

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


No

> 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?


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


 
Reply With Quote
 
 
 
 
tom_usenet
Guest
Posts: n/a
 
      04-28-2004
On Wed, 28 Apr 2004 06:55:24 GMT, "bobsled" <(E-Mail Removed)> wrote:

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


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.

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


Give it at least one virtual function, and document how that should be
overridden. You will almost always want a virtual destructor too.

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


No, you have to explicitly declare it to be 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?


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
 
Reply With Quote
 
 
 
Reply

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
derive from std::set, const_iterator does not work Markus Dehmann C++ 2 06-08-2008 06:16 AM
GeneratorExit should derive from BaseException, not Exception Chad Austin Python 0 08-20-2007 10:00 PM
Best Practices- how to derive full Email address from Username JC ASP .Net 0 08-28-2004 03:03 AM
Can't derive from abstract web page Henke ASP .Net 4 08-13-2004 02:34 PM
Not able to derive from abstract class Amil ASP .Net 1 11-20-2003 11:16 AM



Advertisments