I was trying to understand the implementation code of "final" in cpp:
following is the code:
/* A program with compilation error to demonstrate that Final class cannot
be inherited */
class Final; // The class to be made final
class MakeFinal // used to make the Final class final
{
private:
MakeFinal() { cout << "MakFinal constructor" << endl; }
friend class Final;
};
class Final : virtual MakeFinal
{
public:
Final() { cout << "Final constructor" << endl; }
};
class Derived : public Final // Compiler error
{
public:
Derived() { cout << "Derived constructor" << endl; }
};
int main(int argc, char *argv[])
{
Derived d;
return 0;
}
Output: Compiler Error
In constructor 'Derived::Derived()':
error: 'MakeFinal::MakeFinal()' is private
In this I could not understand the logic of virtually inheriting the MakeFinal class. We could simply have inherited it(makeFinal class) as public and even in that case we would have not been able to inherit it further(because the constructor of Makefile is private and only Final class being its friend could have the access of it).
Any pointer??