RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Imitate Sealed Classes and Disable Object Copying

Java and C# have keywords that allow you to disable inheritance, but C++ does not. Find out how you can still prevent undesirable inheritance from concrete classes and how you can disable the copying of objects that aren't meant to be copied.

he keywords final and sealed in Java and C# respectively ensure that a certain class shall not be subclassed. C++ doesn't have a similar keyword. Naive programmers are often tempted to derive from classes such as std::string and std::vector, which clearly weren't meant to be used as base classes. These illicit derivations can cause undefined behavior. Find out how to implement a compile-time constraint that blocks subclassing—plus, a technique for blocking object copying.

How can you disable inheritance from a class that isn't meant to be derived from? How do you prevent the copying of objects that aren't meant to be copied?

Restrict the access of your special member functions.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date