Derive or not to

B

bobsled

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!
 
S

Sharad Kala

bobsled said:
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_faq2.html#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
 
T

tom_usenet

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top