C ++ object without subclasses?

I was wondering if there is a way to declare an object in C ++ so that it is not subclassed. Is there an equivalent to declaring a destination in Java?

+6
c ++
source share
6 answers

From C ++ Frequently Asked Questions, Inheritance Section

This is known as creating a final or leaf class. There are three ways to do this: a simple technical approach, an even simpler non-technical approach, and a slightly more complicated technical approach.

The technical approach (simple) is to make private class constructors and use the Named Constructor Hierarchy to create objects. No one can create objects of a derived class since the constructor of the base class will not be available. "Named constructors" can return by pointer if you want your objects to be allocated new , or they can return by value if you want objects created on the stack .

A (even simpler) non-technical approach is to put a big thick ugly comment next to the class definition. A comment might say, for example, // We'll fire you if you inherit from this class or even just /*final*/ class Whatever {...}; . Some programmers refuse this, because it is forced by people, not technology, but do not hit its face in meaning: it is very effective in practice.

A somewhat complicated technical approach is to use virtual inheritance . Since the most derived ctor class requires a direct invocation of the virtual base class ctor , the following ensures that no concrete class can inherit from the Fred class:

  class Fred; class FredBase { private: friend class Fred; FredBase() { } }; class Fred : private virtual FredBase { public: ... }; 

The Fred class can access the FredBase ctor, since Fred is a friend of FredBase , but no class from Fred can access the FredBase ctor and, therefore, no one can create a specific class derived from Fred .

If you are in extremely spatially limited environments (such as an embedded system or a handheld computer with limited memory, etc.), you should keep in mind that the above technique can add the memory word in sizeof(Fred) . This is because most compilers implement virtual inheritance by adding a pointer to objects in a derived class. It is compiler specific; Your mileage may vary.

+13
source share

No, this is not necessary. If your class does not have a virtual destructor, in any case, nothing will come of it. So do not give it.

You can use this trick copied from the Stroustrup Frequently Asked Questions :

 class Usable; class Usable_lock { friend class Usable; private: Usable_lock() {} Usable_lock(const Usable_lock&) {} }; class Usable : public virtual Usable_lock { // ... public: Usable(); Usable(char*); // ... }; Usable a; class DD : public Usable { }; DD dd; // error: DD::DD() cannot access // Usable_lock::Usable_lock(): private member 

In C ++ 0x (and as an extension in MSVC) you can make it pretty clean:

 template <typename T> class final { private: friend T; // C++0x, MSVC extension final() {} final(const final&) {} }; class no_derived : public virtual final<no_derived> // ah, reusable {}; 
+7
source share

NO .

Closest you can declare private constructors, and then provide a static factory method.

+2
source share

For C ++, in C ++ there is no direct equivalent language construct.

The usual idiom to achieve this technically is to declare your constructor private. To create an instance of such a class, you need to define an open static factory method.

+2
source share

With C ++ 11, you can add the final keyword to your class, e.g.

 class CBase final { ... 

The main reason I can wish for this (and the reason I was looking for this question) is to mark the class as non-subclasses so that you can safely use a non-virtual destructor and generally avoid vtable.

+1
source share

There really is no way. The best you can do is make all your member functions non-virtual, and all your member variables private, so there are no benefits to subclassing the class.

-one
source share

All Articles