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. VSCode C/C++ 开发环境配置 详细教程

    本博客已暂停更新,需要请转新博客http://www.whbwiki.com/335.html VsCode是一个轻量级的编辑器,但是配置有点复杂,这里我做了C/C++开发环境配置总结,适用于wind ...

  2. 深入剖析 RocketMQ 源码 - 消息存储模块

    一.简介 RocketMQ 是阿里巴巴开源的分布式消息中间件,它借鉴了 Kafka 实现,支持消息订阅与发布.顺序消息.事务消息.定时消息.消息回溯.死信队列等功能.RocketMQ 架构上主要分为四 ...

  3. MySQL高级篇 | 分析sql性能

    在应用的的开发过程中,由于初期数据量小,开发人员写 SQL 语句时更重视功能上的实现,但是当应用系统正式上线后,随着生产数据量的急剧增长,很多 SQL 语句开始逐渐显露出性能问题,对生产的影响也越来越 ...

  4. 讲分布式唯一id,这篇文章很实在

    分布式唯一ID介绍 分布式系统全局唯一的 id 是所有系统都会遇到的场景,往往会被用在搜索,存储方面,用于作为唯一的标识或者排序,比如全局唯一的订单号,优惠券的券码等,如果出现两个相同的订单号,对于用 ...

  5. Linux常见目录结构

    目录 描述 /home 包含Linux系统上各用户的主目录,子目录默认以该用户名命名 /etc 包含Linux系统上大部分的配置文件,建议修改配置文件之前先备份 /var 该目录存放不经常变化的数据, ...

  6. Django笔记&教程 5-2 进阶查询——Queryset

    Django 自学笔记兼学习教程第5章第2节--进阶查询--Queryset 点击查看教程总目录 Queryset相关内容其实蛮多的,本文只介绍一些常用的,详细的推荐查询官方文档:queryset-a ...

  7. etcd install & configuration

    目录 概述 先决条件 相关术语 ETCD 部署 源码安装 其他方式 安装报错 配置文件详解 etcdctl 使用 日志独立 概述 etcd 是兼具一致性和高可用性的键值数据库,为云原生架构中重要的基础 ...

  8. Eclipse使用JDBC方式连接SQLServer2008

    JDBC_连接数据库一.配置 (一)  通过SQL Server配置管理器配置相关部分: 右键点击,启动tcp/ip协议右键点击属性查看自己的TCP端口号,记住,后面会用到右键点击SQL Server ...

  9. [Aizu2993]Invariant Tree

    若$(i,j)\in E$,实际上会不断推出$(p_{i},p_{j})\in E,(p_{p_{i}},p_{p_{j}})\in E,...$ 考虑将$i$向$p_{i}$连边得到了一张(由若干个 ...

  10. git使用大全

    创建四个分支: 查看分支:git branch查看仓库里面所有的分支 git branch -a刷新分支 git remote update origin --prune创建分支:git branch ...