• Simulating final class in C++


      Ever wondered how can you design a class in C++ which can’t be inherited. Java and C# programming languages have this feature built-in. You can use final keyword in java, sealed in C# to make a class non-extendable.

      Below is a mechanism using which we can achieve the same behavior in C++. It makes use of private constructor, virtual inheritance and friend class.

      In the following code, we make the Final class non-inheritable. When a class Derived tries to inherit from it, we get compilation error.

      An extra class MakeFinal (whose default constructor is private) is used for our purpose. Constructor of Final can call private constructor of MakeFinal as Final is a friend of MakeFinal .

      Note that MakeFinal is also a virtual base class. The reason for this is to call the constructor of MakeFinal through the constructor of Derived, not Final (The constructor of a virtual base class is not called by the class that inherits from it, instead the constructor is called by the constructor of the concrete class).

     1 /* A program with compilation error to demonstrate that Final class cannot be inherited */
     2 #include <iostream>
     3 using namespace std;
     4 
     5 class Final;  // The class to be made final
     6 
     7 class MakeFinal // used to make the Final class final
     8 {
     9 private:
    10     MakeFinal() 
    11     { 
    12         cout << "MakFinal constructor" << endl; 
    13     }
    14     friend class Final;
    15 };
    16 
    17 class Final : virtual MakeFinal
    18 {
    19 public:
    20     Final() 
    21     { 
    22         cout << "Final constructor" << endl; 
    23     }
    24 };
    25 
    26 class Derived : public Final // Compiler error
    27 {
    28 public:
    29     Derived() 
    30     { 
    31         cout << "Derived constructor" << endl; 
    32     }
    33 };
    34 
    35 int main(int argc, char *argv[])
    36 {
    37     Derived d;
    38     return 0;
    39 }

      Output: Compiler Error

      In constructor 'Derived::Derived()': error: 'MakeFinal::MakeFinal()' is private

      In the above example, Derived‘s constructor directly invokes MakeFinal’s constructor, and the constructor of MakeFinal is private, therefore we get the compilation error.

      You can create the object of Final class as it is friend class of MakeFinal and has access to its constructor.

      For example, the following program works fine.

     1 /* A program without any compilation error to demonstrate that instances of the Final class can be created */
     2 #include <iostream>
     3 using namespace std;
     4 
     5 class Final;
     6 
     7 class MakeFinal
     8 {
     9 private:
    10     MakeFinal() 
    11     { 
    12         cout << "MakeFinal constructor" << endl; 
    13     }
    14     friend class Final;
    15 };
    16 
    17 class Final : virtual MakeFinal
    18 {
    19 public:
    20     Final() 
    21     { 
    22         cout << "Final constructor" << endl; 
    23     }
    24 };
    25 
    26 int main(int argc, char *argv[])
    27 {
    28     Final f;
    29     return 0;
    30 }

      Output: Compiles and runs fine

      MakeFinal constructor
      Final constructor

      

      Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

      转载请注明:http://www.cnblogs.com/iloveyouforever/

      2013-11-27  14:20:31

  • 相关阅读:
    python列表--查找集合中重复元素的个数
    python3-打印一个进度条
    python3-sys模块
    python3-字符串操作
    python3-深浅复制
    python3-os模块
    接口和抽象类有什么区别
    集合知识
    面向对象的特征有哪些方面
    javadoc时候乱码-编码 GBK 的不可映射字符
  • 原文地址:https://www.cnblogs.com/iloveyouforever/p/3445599.html
Copyright © 2020-2023  润新知