Create a class which can not be inherited

Hi,
Could we write any class which can not be inherited
No. All classes are derivable. You can, however, make it unsafe to derive from a class by not making the destructor virtual. Then derived classes will not be able to safely and polymorphically free resources.
As far as I am aware this can only be achieved using a fudge. I don't believe that there is anything in the language designed to prevent subclassing.
Google "final class C++"

None of the methods are all that great, but if you're desperate, anything is better than nothing.

(One way is to make all constructors private, then write a static "Create" method that returns a new instance.)
I have listen many discussion about "class which can not be inherited". I got answer also if we
declare constructor as private than no one can inherited class.

But did anyone find any practical example or scenario which they use "final class". I am working
on large code base but I did not find any example
i think i need to correction . I got scenario where we need final class in practical scenario.

http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4143
You can make a class non inheritable by making the constructor private. This way child classes will not be able to create objects. But to create the parent class object you can give any static method as an interface to create an object and return an instance. singleton classes usually does this.
As per agoswami's link: I don't think that errors in the design of the STL qualify for making changes to the language.

Personally, I think final classes are a mistake. The purpose of the class system is to allow future needs.

While C++ and Java have some superficial similarities, they really are different languages...
I don't consider STL to be in error by not declaring destructors as virtual. Rather, it follows the C++ mantra
of not making the user pay for features they don't use. Introducing a virtual destructor introduces a
vtable, which causes all users to pay for an extra pointer even if they aren't using the container polymorphically.

Sutter and Alexandrescu, in C++ Coding Standards, argue that you should prefer composition to inheritance.
In practice, this means writing a bunch of forwarding functions to "simulate" inheritance, which will result in
no overhead so long as the forwarders are inlined.
singleton is NOT a way to prevent inheritance. it is a way to prevent more than one instantiation.
private constructor can not prohibit inheritance either. it just disallows any instantiation of higher hierarchy class.
No matter what, you can always inherit,
but you can not always get instantiation from inheritance.
such like the following.
inheritance is always fine. No way to prevent it.
What can be prohibited, is the instantiation.
1
2
3
4
5
6
7
8
9
10
11
12
13
class nonder{
private:
  nonder(){}
  nonder(const nonder&);
  nonder& operator=(const nonder&);
};

class child:public nonder{};

int main(int argc, char* argv[]){
child* p;
return 0;
}

May I ask what good is deriving a class from a base if I cannot instantiate the derived?
little bit correction in this statement
"I don't think that errors in the design of the STL qualify for making changes to the language."

ya that's true it is not error in STL Design. I think it was purpose to write a final class that another
person should not do any mistake.

I agree with jsmith question "May I ask what good is deriving a class from a base if I cannot instantiate the derived?"

I think that was primary question for me also.

I got how can be write the final class in c++ code.
but I am looking for practical scenario where we need final class in the code base.
"need a final class in the code base" is equivalent to saying "don't want someone to
derive from it". The only real reason you wouldn't want someone to derive from
a class is because the class does not have a virtual destructor and therefore will not
work correctly in a polymorphic context.
Hi,

I didn't say that singletons are to prevent inheritance. I just gave an example of providing an interface to create an instance instead of a constructor so that objects can be instantiated.

To everid: inheritance is always there, but when we prevent a child class instantiation literally means the child class is useless and the base class can't be inherited.
#include<iostream.h>
#include<conio.h>

class SealedClass
{
private:
SealedClass(){}

};

class Inherited //will produce an Error SealedClass::SealedClass() is not accessible
{
public:
Inherited() {};
};

void main()
{

}
Last edited on
But I can't instantiate Inherited, so what good is it?
It surprises me this link hasn't came up yet.
http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.11
Topic archived. No new replies allowed.