Question

How to determine if a class has no class deriving from it? Any type-traits methods help?

I want to write out some data. The data is created via a pointer like

unique_ptr<A> a(new A);

If class A has no sub-class, I just write out the data content. If A has subclasses, I will write out its concrete type first, and then write the data content. When reading the data, after reading the concrete type, I know using what concrete type to create the pointer and then read the data content. Thus, I think I have to determine if A has subclass or not?

Was it helpful?

Solution

In C++11, you can declare a class final. This makes sure that no class can derive from it. See http://en.cppreference.com/w/cpp/language/final.

OTHER TIPS

Thats not possible, because a class doesn't know what (If exists) class derives from it.

What you could check is if a class derives from another:

struct foo {};

struct bar : foo {};

static_assert( std::is_base_of<foo,bar>::value );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top