Inhibit Derivation from a C++ Class

Inhibit Derivation from a C++ Class

If no other classes can derive from a class, then you know that all the pointers to that class point to objects of a uniform size. This can simplify memory management and file storage. The idea is to first define a dummy class with a private default constructor. Then virtually derive all the other classes for which you want to inhibit derivation from the dummy class and declare them as friends of the dummy class:

 class CDummy{private:  //Private default constructor  CDummy() {}  friend class CX1;  friend class CX2;  //...};

The class CDummy is a virtual base for the classes CX1, CX2.

 class CX1 : virtual public CDummy{public:  //Constructor, can invoke CDummy's private constructor.  CX1() {}};

The constructor of CDummy is private, but CX1 is a friend, so it can invoke that private constructor. But here’s what happens if you try to derive a class CY1 from CX1:

 class CY1 : public CX1{public:  //Constructor, cannot invoke CDummy's private constructor  CY1() {}};

Because CY1 has CDummy as a virtual base, it must initialize it, but it cannot. You get a compile time error, for example in the case of the VC++ compiler you get the message:

 "'CDummy::CDummy' : cannot access private member declared in class'CDummy'"
Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of