Kiran's Blog

Ramblings from a dehydrated mind…

  • Archives

    • 14,907
  • Advertisements

Sealed class…

Posted by kiraninbng on July 3, 2006

One way to create a sealed class is to make the constructor private and use a friend class.

Ex :

class BaseClass
 virtual ~BaseClass();

     friend class DerivedClass;

class DerivedClass : private virtual BaseClass 
 virtual ~DerivedClass();

Now since the constructor of baseclass is not accesible to any class other than the friend class,the derived class cannot be inherited by any other class.

I know there are other ways of acheiving the same,if you know them do pitch in with your suggestiions.


3 Responses to “Sealed class…”

  1. Farpointer said

    What does that “private virtual” keyword mean .
    Is it Derived class cannot be inherited or the base class canot be inhertited by anyone other than the friend class .
    I dont know any other way of doing this , one thing that closely resembles this is by making a pure virtual function in the base class so that nobody can make a direct object of the class .

  2. kiraninbng said

    private inheritence is basically inheritence of implementation.All the public/protected data from the base class will become private in the derived class.virtual signifies that there can be only one instance of the object in the derived heiarchy and also the most derived class has to invoke all the base class constructors.

  3. Farpointer said

    ok , thanx.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: