How to define a private class in C ++?

How to stop a class to inherit from another class.

+53
c ++ inheritance class sealed derived-class
Jan 17 '11 at 12:12
source share
6 answers

C ++ 11 Solution

In C ++ 11, you can pin a class using the final keyword in a definition like:

 class A final //note final keyword is used after the class name { //... }; class B : public A //error - because class A is marked final (sealed). { // so A cannot be derived from. //... }; 

To find out other use cases for final, see my answer here:

  • What is the purpose of the "final" keyword in C ++ 11 for functions?



Solution C ++ 03

Bjarne Stroustrup code : Can I stop people getting my class?

 class Usable; class Usable_lock { friend class Usable; private: Usable_lock() {} Usable_lock(const Usable_lock&) {} }; class Usable : public virtual Usable_lock { public: Usable(); Usable(char*); }; Usable a; class DD : public Usable { }; DD dd; // error: DD::DD() cannot access // Usable_lock::Usable_lock(): private member 



Generic_lock

So, we can use the template to make Usable_lock general enough to print any class:

 template<class T> class Generic_lock { friend T; Generic_lock() {} //private Generic_lock(const Generic_lock&) {} //private }; class Usable : public virtual Generic_lock<Usable> { public: Usable() {} }; Usable a; //Okay class DD : public Usable { }; DD dd; //Not okay! 
+85
Jan 17 '11 at 12:12
source share

There are two ways: simple cheap and right. Two answers from @Naveen and @Nawaz concern the correct one, requiring the manual creation of a sealant class for each class that you really want to seal.

The invalid method used by adobe libraries uses a template class for this. The problem is that you cannot declare the template argument as a friend, which means that you need to switch from private to less secure protected :

 template <typename T> class sealer { protected: sealer() {} }; class sealed : virtual sealer<sealed> {}; 

And you can automate it with a macro (I don’t remember the exact taste of the macro in Adobe code):

 #define seal( x ) virtual sealer<x> class sealed : seal(sealed) {}; 

Now this will catch people who mistakenly try to inherit, not knowing that they should not:

 class derived : sealed {}; int main() { derived d; // sealer<T>::sealer() is protected within this context } 

But it will not prevent people who really want to extract from them, since they can access the constructor based on the templates themselves:

 class derived : sealed, sealer<sealed> {}; int main() { derived d; }; 

I'm not sure if this will change in C ++ 0x, I think I recall some discussions about whether the class template is allowed to make friends with one of its arguments, but in a cursory search on the project I can’t say If it were allowed, this would be a great general solution:

 template <typename T> class sealer { sealer() {} friend class T; // Incorrect in C++03 }; 
+10
Jan 17 2018-11-12T00:
source share

C ++ 11 adds the ability to prevent inheritance from classes or simply prevent overriding methods in derived classes. This is done using the special identifier final . For example:

 class Base final { }; class Derived1 : Base { }; // ill-formed because the class Base has been marked final 

or

 class Base { virtual void f() final; }; class Derived : Base { void f(); // ill-formed because the virtual function Base::f has been marked final 

Note that final is not a language keyword. This is technically an identifier; it is of particular importance when used in these specific contexts. In any other place, it may be a valid identifier.

+8
Aug 11 2018-11-11T00:
source share

Based on the Bjarne Stroustrup http://www.stroustrup.com/bs_faq2.html#no-derivation FAQ with a little modification without using a friend keyword:

 // SEALED CLASS DEFINITIONS class Usable_lock { protected: Usable_lock() {} Usable_lock(const Usable_lock&) {} }; #define sealed_class private virtual Usable_lock // SEALED CLASS USAGE EXMAPLES class UsableLast : sealed_class { public: UsableLast(){} UsableLast(char*){} }; class DD : public UsableLast {}; // TEST CODE template <class T> T createInstance() { return T(); } int main() { createInstance<UsableLast>(); // createInstance<DD>(); return 0; } 
0
Dec 29 '15 at 16:43
source share

The following code shows how to define a private class in VS 2010.

 class A sealed { //here goes the class code }; class B : public A { }; 

Now B: cannot inherit from A since it was declared as "sealed". Also a detailed explanation about the private keyword can be found here http://msdn.microsoft.com/en-us/library/0w2w91tf.aspx

-one
Apr 01 '14 at 5:24
source share

You can not. C ++ is not Java or C #. And also there is no point, ever, IMHO.

-eleven
Jan 17 '11 at 12:13
source share



All Articles