Sealing Classes in C++ ---- by Francis Xavier
来源:互联网 发布:zm400条码机软件 编辑:程序博客网 时间:2024/06/06 07:22
Introduction
Some recent languages like C# and Java allow you to seal your classes easily using a keyword like sealed or final respectively. C++ doesn't have any such keyword for this purpose. However, it's possible to still do it using a trick: When using virtual inheritance, the initialization list of the most-derived-class's constructor directly invokes the virtual base class's constructor. This means that if we can hide access to the virtual base class's constructor, then we can prevent any class from deriving from it. This mimics the effect of being sealed.
Solution Attempt #1
To provide an easy way to seal classes, we can write a header file Sealed.h like this:
class SealedBase{protected: SealedBase() { }};#define Sealed private virtual SealedBaseNow to seal a class, say Penguin, we just need to derive it from Sealed, like this:
#include "Sealed.h"class Penguin : Sealed{};That's it. Penguin is now a sealed class. Let's try deriving another class, BigZ (Surf's Up (2007), anyone?) from Penguin:
class BigZ : Penguin{};BigZ bigZ; // error C2248Instantiating an object of BigZ should yield a compiler error. The MSVC++ 2005 compiler gives me the following error message:
error C2248: 'SealedBase::SealedBase' : cannot access inaccessible member declared in class 'SealedBase'
A Serious Flaw
All seems to be working well. However, one of my fellow programmers, Angelo Rohit, pointed out to me that this method has a serious flaw in it. Angelo says that if BigZ derives from Penguin and Sealed, then it will be possible to create objects of BigZ:
class BigZ : Penguin, Sealed{};BigZ bigZ; // OK; no compiler errorWhy does this happen? BigZ derives from Sealed just like Penguin does, which means that it now has access to Sealed's constructor. And since Sealed is inherited virtually by both Penguin and BigZ, there is only one copy of it - which is now also accessible to BigZ. Bummer. We need to have a mechanism by which BigZ is forced to call the constructor of a class which it doesn't have access to.
Solution Attempt #2
After pondering over this for a while, I realized that if we can somehow generate different base classes every time Sealed is derived from, then it would work. Let's rewrite the Sealed.h header to look like this:
template < int T >class SealedBase{protected: SealedBase() { }};#define Sealed private virtual SealedBase<__COUNTER__>What does this do? SealedBase is now a templated class which takes an integer as an argument. __COUNTER__ is a predefined macro which expands to an integer starting with 0 and incrementing by 1 every time it is used in a compiland. So every time Sealed is derived from, it generates a new SealedBase class using the incremental number which __COUNTER__ expands to.
Now let's go back to our BigZ class which derives from both Penguin and Sealed:
class BigZ : Penguin, Sealed{};BigZ bigZ; // error C2248This time around though, BigZ can't escape from the compiler. Penguin derives from SealedBase<number1> and BigZ derives from SealedBase<number2>, where number1 and number2 are two non-identical integers. So now BigZ has to invoke the constructor of SealedBase<number1>, which it doesn't have access to.
The MSVC++ 2005 compiler gives me the following error message:
error C2248: 'SealedBase::SealedBase' : cannot access inaccessible member declared in class 'SealedBase '1> with1> [1> T=01> ]
Portability Issues
However, you might be thinking that since we're using a special predefined macro __COUNTER__ in our implementation, this code is not portable. Well, it's supported by MSVC++ (which I used to test the above code) and also by GCC. But what about compilers which don't?
A Portable Solution
After a little thought, I came up with the following way:
In Sealed.h:
template < class T >class SealedBase{protected: SealedBase() { }};#define Sealed(_CLASS_NAME_) private virtual SealedBase<_CLASS_NAME_>And to seal a class:
#include "Sealed.h"class Penguin : Sealed(Penguin){};When sealing a class, we need to mention that class's name to the Sealed macro. This enables the Sealed macro to generate a new version of SealedBase. This is less elegant than simply having to derive from Sealed, but is more portable, making it a good alternative for compilers which don't support the __COUNTER__ predefined macro.
Final Words
People who use MSVC++ or GCC can simply use Solution Attempt #2, as it is cleaner. People on other compilers, can use the Portable Solution. If you have any questions, suggestions, improvements, or simply want to say hi, please email me at francisxavierjp [at] gmail [dot] com.
Thanks for Reading!
Francis Xavier
References
[1] C++ Q&A: List View Mode, SetForegroundWindow, and Class Protection
[2] Vladislav Lazarenko: "[boost] Sealed C++ class."
- Sealing Classes in C++ ---- by Francis Xavier
- Chapter12. Foundation Classes and selector in Objective-C
- Caused by: java.lang.SecurityException: sealing violation: package org.apache.derby...
- Classes in JScript
- classes in java
- Sealed Classes in C#
- static classes in C#
- ViewController Classes in UIKit
- COM in plain C By Jeff Glatt.
- get password in terminal by C
- COM in plain C By Jeff Glatt.
- Coroutines in C(by Simon Tatham)
- Abstract Classes (C++)
- C#Partial Classes
- C Storage Classes
- Objective-C Runtime Classes
- Why should you choose fstream classes for file I/O in C++?
- Creating Collection Classes in C#
- Second Life --- lsl script 学习(1)
- javascript正则表达式取汉字的长度,一个汉字占两个字节长度
- 任务
- Second Life --- lsl script 学习(2)
- 转贴 JS常用语句
- Sealing Classes in C++ ---- by Francis Xavier
- 一套.net窗体身份验证方案(解决了防止用户重复登陆,session超时等问题)
- c++资源之不完全导引
- 长文章分页
- AJAX解析XML
- VS2005 点滴起步 ---部分DataGridView 属性 奇偶行颜色变换的属性
- Visual Studio 2005 IDE 技巧和窍门
- asp.net常用代码
- Sales Channels of Hong Kong Clothing Industry