How to define a class can't be inheritance? Like "final" Keyword in Java?
It can be implement in C++? Thank u.
I think it's possible by making the ctor private, however you have to provide an alternative ctor, which may or may not suit your programming style.
1 2 3 4 5 6 7
|
class FinalClass
{
public:
static FinalClass* CreateInstance() { return new FinalClass; }
private:
FinalClass() {}
};
|
It can be implement in C++? |
It can't be implemented in C++. The best way is to write a comment above a final class:
1 2
|
/*Do not inherit from this class*/
class my_final_class {/*...*/};
|
You might try some other magic, which makes the child class unusable somehow, but the above method works the best.
Topic archived. No new replies allowed.