Kiran's Blog

Ramblings from a dehydrated mind…

  • Archives

    • 14,897

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
{
private:
 BaseClass();
 virtual ~BaseClass();

     friend class DerivedClass;
};

class DerivedClass : private virtual BaseClass 
{
public:
 DerivedClass();
 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.

Advertisements

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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

 
%d bloggers like this: