<

Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x

Sealing a C++ Class

Published on
11,288 Points
5,188 Views
1 Endorsement
Last Modified:
evilrix
An expert in cross-platform ANSI C/C++ development, specialising in meta-template programming and low latency scalable architecture design.
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism

The trick is to virtually inherit from a base class where the constructor is private and the sub-class is declared a friend in said base class. If you then try to sub-class the pseudo sealed class the compiler will not be able to synthesize a callable constructor for the virtual base class so instantiation will fail.

It works by making the default constructor of the sealer class private, which means nothing can construct it. We then make the class we want to seal a friend of the sealer class and subclass it with virtual inheritance. As the subclass is a friend of the sealer class it can call the private constructor so we are able to instantiate instances of it. Since we virtually inherited the sealer class and since in C++ the top most sub-class of an inheritance tree always called the base classes constructor directly the fact that this constructor is inaccessible means the compiler will produce an error. Voila, we have sealed the class to prevent it being sub-classed.

The following code example uses a macro called SEALED, which takes care of creating a virtual base class and making the real class virtually derive from it.
#define SEALED(className) \
	className ## Sealer \
		{ \
			private: className ## Sealer(){}; \
			friend class className; \
		}; \
		class className : virtual private className ## Sealer
 
class SEALED(MyClass) {};
 
class MyClassDisallowed : public MyClass {};
 
int main()
{
	// Perfectly legal construction
	MyClass myClass;
 
	// Illegal construction, super-class is sealed
	MyClassDisallowed myClassDisallowed;
}

Open in new window

1
Comment
Author:evilrix
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 51

Expert Comment

by:Ted Bouskill
Personally I prefer this solution to sealing classes using a template:
http://www.codeproject.com/KB/cpp/SealingCppClasses.aspx
0
 
LVL 40

Author Comment

by:evilrix
>> Personally I prefer this solution to sealing classes using a template:
With the template way of doing this all the sealer classes are defined within the namespace of the template. Although I generally advocate not using preprocessor macros, in this instance it means the sealer class shares the name of the class being sealed and the sealer class will be in the same namespace.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Join & Write a Comment

The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month