dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Beginner
Apr 10, 2002

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


Preventing Derivation


In general, disabling inheritance is a dubious idea. Yet under some circumstances it may be useful. To do this, declare the class in question (called A in our example) as a friend of another class (called Lock in our example). Locks constructor must be private:
 
class A;

class Lock {
 friend class A;
private:
 Lock() {}
 };

Next, virtually derive the class in question from Lock:
 
class A : virtual public Lock {
  // ...
public:
 A();
 A(int);
//.. additional ctors, dtors etc.
};

Consequently, any attempt to derive from A will fail:
 
A a; // OK
class Derived : public A { };
Derived d;// error: Deriveds ctor cant access Locks ctor
Danny Kalev
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date