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

Simulating final class in C++的更多相关文章

  1. java抽象、接口 和final

    抽象 一.抽象类:不知道是具体什么东西的类. abstract class 类名 1.抽象类不能直接new出来. 2.抽象类可以没有抽象方法. public abstract class USB { ...

  2. Java内部类final语义实现

    本文描述在java内部类中,经常会引用外部类的变量信息.但是这些变量信息是如何传递给内部类的,在表面上并没有相应的线索.本文从字节码层描述在内部类中是如何实现这些语义的. 本地临时变量 基本类型 fi ...

  3. Java关键字final、static

    一.final根据程序上下文环境,Java关键字final有“这是无法改变的”或者“终态的”含义,它可以修饰非抽象类.非抽象类成员方法和变量.你可能出于两种理解而需要阻止改变:设计或效率. final ...

  4. JavaSE 之 final 初探

    我们先看一道面试题: 请问 final 的含义是什么?可以用在哪里?其初始化的方式有哪些? 首先我们回答一下这道题,然后再探究其所以然.  1.final 表示“最终的”.“不可改变的”,意指其修饰类 ...

  5. PHP的final关键字、static关键字、const关键字

    在PHP5中新增加了final关键字,它可以加载类或类中方法前.但不能使用final标识成员属性,虽然final有常量的意思,但在php中定义常量是使用define()函数来完成的. final关键字 ...

  6. final修饰符

    final本身的含义是"最终的,不可变的",它可以修饰非抽象类,非抽象方法和变量.注意:构造方法不能使用final修饰,因为构造方法不能被继承,肯定是最终的. final修饰的类: ...

  7. 浅析Java中的final关键字(转载)

    自http://www.cnblogs.com/dolphin0520/p/3736238.html转载 一.final关键字的基本用法 在Java中,final关键字可以用来修饰类.方法和变量(包括 ...

  8. java关键字extends(继承)、Supe(父类引用空间)、 This(方法调用者对象)、Instanceof(实例类型-判断对象是否属于某个类)、final(最终)、abstract(抽象) 、interface(接口)0

    java 继承使用关键字extends   继承的作用:减少代码量,优化代码 继承的使用注意点: 1子类不能继承父类的私有变量 2.子类不能继承父类的构造方法 3.子类在调用自己的构造方法时 会默认调 ...

  9. files list file for package 'xxx' is missing final newline

    #!/usr/bin/python # 8th November, 2009 # update manager failed, giving me the error: # 'files list f ...

随机推荐

  1. 【java+selenium3】Tesseract-OCR识别图片验证码 (十六)

    [java+selenium+Tesseract-OCR(图片识别)+AutoIt(windows窗口识别)]完成自动化图片验证码识别! 一.AutoIt(windows窗口识别)参考:https:/ ...

  2. 解决create-react-app 后 npm start or yarn start 中出现 的webpack版本问题

    解决create-react-app 后 npm start or yarn start 中出现 的webpack版本问题 错误提示信息 There might be a problem with t ...

  3. Unicode、UTF-8、UTF-16 终于懂了

    计算机起源于美国,上个世纪,他们对英语字符与二进制位之间的关系做了统一规定,并制定了一套字符编码规则,这套编码规则被称为ASCII编码 ASCII 编码一共定义了128个字符的编码规则,用七位二进制表 ...

  4. [cf1495F]Squares

    令$nex_{i}=\min_{i<j,p_{i}<p_{j}}j$(即$i$的第2类边),若不存在此类$j$则$nex_{i}=n+1$ 建一棵树,其以0为根,且$1\le i\le n ...

  5. [noi712]练级

    先考虑一个联通块,可以发现这个联通快内不会存在两个偶数的点证明:如果存在,那么这两个点的某一条路径上的边全部反过来,可以使答案+2,即答案为点数或点数-1同时,发现答案的奇数点数一定与边数同奇偶,那么 ...

  6. [loj3274]变色龙之恋

    首先有一个暴力的做法,将任意两个点判断,可以得到与之相关的1或3只变色龙:1只是两只变色龙相互喜欢,那么剩下那只就是颜色相同:3只从3只选2只并和自己判断一次,结果为1的那次剩下的那个就是他喜欢的,然 ...

  7. linux下root用户和普通用户切换

    没有root权限干啥都费劲 切换root用户 sudo su 切换普通用户 su player3

  8. mybatis-批量操作数据(list对象 )

    在实际工作中老是忘记 传入的参数和数据库参数名称要一致还是与实体类型一致导致很多笑话发生. 那我还是做个记录吧! dao层: int addRemark(@Param("list" ...

  9. WebRTC与CSS滤镜(CSS filter)

    我们知道了如何使用WebRTC打开摄像头,可以截取视频帧并且用canvas显示出来. 本文将滤镜与视频结合.给视频加上一层滤镜.主要使用到的是filter属性. canvas与滤镜 先来看filter ...

  10. AT4168 [ARC100C] Or Plus Max

    从\(whk\)回来了. 考虑我们需要维护一个子集的信息. 对于二进制的子集信息维护有一个很经典的操作: 高维前缀和. AT4168 [ARC100C] Or Plus Max // Problem: ...