Going under the assumption that there is a legitimate reason for preventing derivation from some class, Bjarne gives a solution here for the answer to "Can I stop people deriving from my class?"
However, I thought of:
class final {
protected:
final() { } // line 3
};
class B : private virtual final {
};
class D : public B { // line 9
};
int main() {
B b;
D d; // line 14
}
When trying to compile, one gets:
foo.cpp: In constructor ‘D::D()’:
foo.cpp:3: error: ‘final::final()’ is protected
foo.cpp:9: error: within this context
foo.cpp: In function ‘int main()’:
foo.cpp:14: note: synthesized method ‘D::D()’ first required here
Does my solution work for all cases? IMHO, it's better because the class final
is generic and doesn't require foreknowledge of the class to prevent derivation from.
The solution is rather bad, it could be improved with CRTP (adobe does so) but it won't be a complete solution. The problem with your code is that another programmer that does not want to break your contract (she is a good person) but does not know that she should not derive from your class might want to lock others from deriving from her own class:
class YourSealedClass : private virtual final {};
class HerSealedClass : public YourSealedClass, private virtual final {
};
Note that there is not even malign intention, and the contract is broken. The improvement with CRTP would be:
template <typename T>
class seal
{
protected:
seal(){}
~seal(){}
};
class YourSealedClass : private virtual seal<YourSealedClass> {};
This will catch the previous mistake, as her code would be:
class HerSealedClass : public YourSealedClass, private virtual seal<HerSealedClass> {};
And as she is not inheriting from seal<YourSealedClass>
the compiler will catch it up.
The problem with this approach is that it does not block some stubborn (or malign) programmer from writing:
class MalignClass : public YourSealedClass, private virtual seal<YourSealedClass> {};
To become a child of the sealing class, and as such gain access to your own class.
In C++0x I believe (would have to recheck it) that they will raise the restriction that a template cannot befriend a template argument, and that will be a good step into a generic typesafe solution for a class sealer:
template <typename T>
class seal {
friend class T; // illegal in C++03
seal() {};
~seal() {};
};
class MySealedClass : private virtual seal<MySealedClass>
{};
The advantage of this approach (currently unavailable) is that because the constructor and destructors are private only friends can instantiate the type. Because you are using the CRTP to inherit from an specific instantiation passing your own type as argument, you are telling the compiler that only you can actually instantiate your base, and all pieces fall right into place.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With