Prevent class inheritance in C++ -
Recently one of my friends asked me that C ++ wanted to thwart the compilation.
I was thinking about it and got 3 answers. Not sure which is the best.
1) Private Creator (s)
Class CBase {Public: Fixed CBase * CreateInstance () {CBBS * B1 = New CBBs (); Return B1; } Private: cb () {} CBbase (CBbase3) {} CBC and operator = (CBBSE End) {}};
2) Using the CSell Base Class, Personal CTOR & amp; Virtual Legacy
class CSealed {Private: CSealed () {} Friend Class CBase; }; Class CBBSE: Virtual CSealed {Public: CBase () {}};
3) Using a CSealed Base Class, Protected CTOR & amp; Virtual Legacy
class CSealed {Secure: CSealed () {}}; Class CBBSE: Virtual CSealed {Public: CBase () {}};
All the above methods ensure that the CBBSS class can not be inherited. My question is:
1) Which is the best way? Are there any other methods available?
2) Method 2 & amp; Why is it so long as the CSealed class inherited in heritage will not work? What does something with vdisp ptr do ??
PS:
The above program was compiled in the MS C ++ Compiler (Visual Studio). Reference:
As C ++, you can add the last keyword to your square, for example For
class CBase final {...
The main reason for this is that I can see to do this (and I look for this question I have come in) to mark a class as non subclassable so that you can safely use a non-virtual constructor and can not completely escape from a Vettel.
Comments
Post a Comment