I want to write a class, which by itself should restrict inheritance,
something like final class in java. Is it possible to write one in c++?

Regards,
Alamelu N

Re: How to restrict inheritance in C++ by M

M
Mon Apr 14 01:06:32 CDT 2008

http://www.research.att.com/~bs/bs_faq2.html#no-derivation

Regards,
Shoaib.

"Alamelu" <Alamelu@discussions.microsoft.com> wrote in message
news:F65E2E2D-390E-48D0-8677-D9147A5416A3@microsoft.com...
>I want to write a class, which by itself should restrict inheritance,
> something like final class in java. Is it possible to write one in c++?
>
> Regards,
> Alamelu N



Re: How to restrict inheritance in C++ by Alamelu

Alamelu
Tue Apr 15 07:11:00 CDT 2008

Hi,

I do not understand the concept behind it. Can you please tell how making
the class constructor private and by having a derived class with virtual base
can restrict inheritance?

And there is also a constrain of having a derived class for a class not to
be inherited

Regards,
Alamelu N

"M. Shoaib Surya" wrote:

> http://www.research.att.com/~bs/bs_faq2.html#no-derivation
>
> Regards,
> Shoaib.
>
> "Alamelu" <Alamelu@discussions.microsoft.com> wrote in message
> news:F65E2E2D-390E-48D0-8677-D9147A5416A3@microsoft.com...
> >I want to write a class, which by itself should restrict inheritance,
> > something like final class in java. Is it possible to write one in c++?
> >
> > Regards,
> > Alamelu N
>
>
>

Re: How to restrict inheritance in C++ by M

M
Tue Apr 15 07:57:09 CDT 2008

This is Stroustrup's solution to restrict inheritance of a class, as C++
does not have a final/sealed keyword like Java/C#.

The key idea is that, if you declare your base class constructor as private,
any class derived from it can not access that constructor. But, then it also
makes the base class itself uninstantiatable. The first workaround for that
is to use a named constructor, but that makes it not-so-clean for the client
class. And you would most probably want declare a class as final/sealed when
someone else would be the user of the class. So, here comes Stroustrup's
solution which is a little more complex for you , but makes it with a better
abstraction level for the user of your class.

You can find a little more explanation on that here:
http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.11

Read more on Private inheritance here:
http://www.parashift.com/c++-faq-lite/private-inheritance.html

Read more on Virtual inheritance here:
http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.9

Hope that will help you understand how that works.

Regards,
Shoaib.

"Alamelu" <Alamelu@discussions.microsoft.com> wrote in message
news:5965513E-1020-414F-85F8-21C10263C999@microsoft.com...
> Hi,
>
> I do not understand the concept behind it. Can you please tell how making
> the class constructor private and by having a derived class with virtual
> base
> can restrict inheritance?
>
> And there is also a constrain of having a derived class for a class not
> to
> be inherited
>
> Regards,
> Alamelu N
>
> "M. Shoaib Surya" wrote:
>
>> http://www.research.att.com/~bs/bs_faq2.html#no-derivation
>>
>> Regards,
>> Shoaib.
>>
>> "Alamelu" <Alamelu@discussions.microsoft.com> wrote in message
>> news:F65E2E2D-390E-48D0-8677-D9147A5416A3@microsoft.com...
>> >I want to write a class, which by itself should restrict inheritance,
>> > something like final class in java. Is it possible to write one in c++?
>> >
>> > Regards,
>> > Alamelu N
>>
>>
>>