Restrict inheritance to desired number of classes at compile-time

前端 未结 4 1605
野趣味
野趣味 2021-01-01 06:55

We have a restriction that a class cannot act as a base-class for more than 7 classes. Is there a way to enforce the above rule at compile-time?

I am aware of Andre

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-01 07:19

    Sorry, I don't know how to enforce any such limit using the compiler.

    Personally I wouldn't bother trying to force the rule into the code itself - you are cluttering the code with stuff that has nothing to do with what the code is doing - it's not clean code.

    Rather than jumping through hoops, I'd try to get that rule relaxed. Instead it should be a guideline that could be broken if necessary and in agreement with others in the team.

    Of course, I lack the knowledge of exactly what you're doing so the rule could be appropriate, but in general it probably isn't.

    Any programming "rule" that says you must never do x or you must always do y is almost always wrong! Notice the word "almost" in there.

    Sometimes you might need more than 7 derived classes - what do you do then? Jump through more hoops. Also, why 7? Why not 6 or 8? It's just so arbitrary - another sign of a poor rule.

    If you must do it, as JP says, static analysis is probably the better way.

提交回复
热议问题