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. 大数据中必须要掌握的 Flink SQL 详细剖析

    Flink SQL 是 Flink 实时计算为简化计算模型,降低用户使用实时计算门槛而设计的一套符合标准 SQL 语义的开发语言. 自 2015 年开始,阿里巴巴开始调研开源流计算引擎,最终决定基于 ...

  2. Jenkins MultiJob

    前提:项目有十几个服务每次发版/更新服务需要一个个去编译 目的:希望能够建立一个任务一次构建可以批量编译很多服务,并且需要输入一个参数指定编译的分支 需要插件: MultiJob 安装插件 1.在Je ...

  3. SpringMVC配置版到注解版

    什么是springmvc? 1.1.什么是MVC MVC是模型(Model).视图(View).控制器(Controller)的简写,是一种软件设计规范. 是将业务逻辑.数据.显示分离的方法来组织代码 ...

  4. ELK集群之kibana(4)

    kibane安装及基础使用 Kibana的安装 Kibana包含前端展示.es操作简化 yum localinstall kibana-7.6.2-x86_64.rpm -y Kibana配置修改ki ...

  5. [atARC114F]Permutation Division

    由于是排列,即任意两个数字都各不相同,因此字典序最大的$q_{i}$就是将每一段的第一个数从大到小排序 接下来,考虑第一个元素,也就是每一段开头的最大值,分类讨论: 1.当$p_{1}\le k$时, ...

  6. [hdu6715]算术

    首先要知道一个式子:$\mu(lcm(i,j))=\mu(i)\cdot \mu(j)\cdot \mu(gcd(i,j))$(分是否为0讨论)令$d=gcd(i,j)$,$n'=\lfloor n/ ...

  7. spring boot 动态生成接口实现类

    目录 一: 定义注解 二: 建立动态代理类 三: 注入spring容器 四: 编写拦截器 五: 新建测试类 在某些业务场景中,我们只需要业务代码中定义相应的接口或者相应的注解,并不需要实现对应的逻辑. ...

  8. poi上传下载

    本教程只实现poi简单的上传下载功能,如需高级操作请绕行! <!--poi start--> <dependency> <groupId>org.apache.po ...

  9. 用idea生成javadoc在线文档

    1.点击需要生成文档的包 2.点击tools--->选择generate javaDoc /1选择输出目录 /2防止中文乱码建议加上: -encoding utf-8 -charset utf- ...

  10. adb server version (32) doesn't match this client (39); killing...解决办法

    输入今天遇到,安装AndroidSDK之后,已经配置好环境变量,输入adb可运行,但是输入adb devices之后就出现adb server version (32) doesn't match t ...