misra c++:2008-规则10-3-1-沿继承层次结构的虚拟函数的定义

发布于 2025-02-14 01:32:20 字数 1466 浏览 0 评论 0原文

据我所知, tl; dr; 该规则提倡不覆盖已经定义的方法(如果不是纯粹的话)。因此,如果我不允许我覆盖已经定义的方法,那么一个人将如何“自定义”子类中的行为以及为什么(尽管“读者/审稿人”的便利性和禁止统治的呼吁,人们是否希望执行这样的规则?


我不知道? t 理解获得规则10-3-1 misra c ++的原因不幸的是,由于版权原因,我可能不允许简单地粘贴该规则的内容,但它看起来很如面:

class Foo
{
  public:
    virtual void foo() = 0;
    virtual void bar() {};
    virtual void baz() = 0;
};

Foo::baz()
{}

class Bar : public Foo
{
  public:
    virtual void foo() {}; // compliant, since foo() is pure virtual in Foo
    virtual void bar() {}; // non-compliant, since bar() already has a definition in Foo
    virtual void baz() {}; // compliant, since baz() indeed already has a definition in Foo but is declared pure
};

它规定必须只有一个定义 < 虚拟成员函数的继承层次结构。

/em>沿着给定的 成员 以下示例。

class Foo
{
  public:
    virtual void foo();
    virtual void bar();
};

class Bar : public Foo
{
  public:
    virtual void foo(); // compliant, explicit virtual
    void bar(); // non-compliant, since implicit virtual
};

函数必须始终为virtual,但 如果未声明纯) 规则10-3-2(指的是用示例覆盖覆盖物违反上一个规则)。

我知道偏差记录的可能性。

我实际上很好奇1) explicit 原因(尽管“读者/审稿人”的便利性 - 似乎非常不满意的恕我直言)是否有理由禁止多态性对象设计的主要好处之一,而2)为什么下一个规则只会展示此规则的反面示例,以及3)当需要为子类的“自定义行为”时,如何避免偏差记录。

也许有人可以为我提供一些启示。


我删除了其他脚注问题,以关注实际问题。

tl;dr; as far as I understand, this rule advocates to not override an already defined method (if not declared pure). So if I am not allowed to override an already defined method, how would one "customise" behaviour in subclasses and why (despite for "readers/reviewers" convenience and prohibiting call by dominance would one want to enforce such a rule?


I don't understand get the reason behind the rule 10-3-1 of Misra C++:2008, which is about the definitions of virtual functions along the inheritance hierarchy. Unfortunately, I'm probably not allowed to simply paste the content of the rule for copyright reasons, but it looks like follows:

class Foo
{
  public:
    virtual void foo() = 0;
    virtual void bar() {};
    virtual void baz() = 0;
};

Foo::baz()
{}

class Bar : public Foo
{
  public:
    virtual void foo() {}; // compliant, since foo() is pure virtual in Foo
    virtual void bar() {}; // non-compliant, since bar() already has a definition in Foo
    virtual void baz() {}; // compliant, since baz() indeed already has a definition in Foo but is declared pure
};

It states that there must be only one definition along the inheritance hierarchy for a given virtual member function. Except, the member function is declared pure but does have a definition as well.

The next rule (10-3-2) states that overriding member functions must always be preceded by virtual, with an example like the following.

class Foo
{
  public:
    virtual void foo();
    virtual void bar();
};

class Bar : public Foo
{
  public:
    virtual void foo(); // compliant, explicit virtual
    void bar(); // non-compliant, since implicit virtual
};

Nevertheless, I don't understand the reason of the rule 10-3-1 (which disallows to "overwrite" an already defined member function if it is not declared pure) in conjunction with the rule 10-3-2 (which refers to overwriting with an example violating the previous rule).

I am aware of the possibility of a deviation record.

I am actually curious 1) what explicit reason (despite for "readers/reviewers" convenience - that seems very unsatisfying IMHO) justifies to disallow one of the primary benefits of polymorphic object-oriented design and 2) why the very next rule just showcases a counter example to this rule and 3) how to avoid a deviation record when "customised behaviour" for subclasses is needed.

Maybe someone can shed some light on this for me.


I removed the additional footnote question to focus on the actual question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

莳間冲淡了誓言ζ 2025-02-21 01:32:20

如果不允许我覆盖已经定义的方法,则如何在子类中“自定义”行为?

通过仅覆盖方法。

纯方法是您的自定义点。覆盖方法已经是自定义行为。不要重新定制它。

如果您发现自己需要覆盖具体的虚拟功能,请将父级分为一分为二。假设您有

class Foo
{
  public:
    virtual void foo() { ... };
    virtual void bar() { ... };
};

,您想覆盖foobar。然后执行此操作:(

class AFoo
{
  public:
    virtual void foo() = 0;
    virtual void bar() = 0;
};

class Foo : public AFoo
{
  public:
    virtual void foo() { ... };
    virtual void bar() { ... };
};

class Bar : public IFoo
{
  public:
    virtual void foo() { ... };
    virtual void bar() { ... };
};

我称提取的类afoo(a抽象)而不是ifoo,因为它确实是不是必须是纯接口。其他您不想覆盖的方法可能会保持在在afoo中。)

这并不总是那么容易如图所示,例如,如果要覆盖层次结构中深处5个继承水平的方法。您将必须复制大量代码,创造性地使用多重继承,或重新设计设计以使用更多的控制和更少的继承。但这就是C ++程序员的生活,他将自己的灵魂卖给Misra 在执行编码标准的行业中工作。

if I am not allowed to override an already defined method, how would one "customise" behaviour in subclasses?

By overriding exclusively pure methods.

Pure methods are your customisation points. Overridden methods are already customised behaviour. Do not re-customise it.

If you find yourself needing to override a concrete virtual function, split the parent class in two. Say you have

class Foo
{
  public:
    virtual void foo() { ... };
    virtual void bar() { ... };
};

and you want to override foo and bar. Then do this:

class AFoo
{
  public:
    virtual void foo() = 0;
    virtual void bar() = 0;
};

class Foo : public AFoo
{
  public:
    virtual void foo() { ... };
    virtual void bar() { ... };
};

class Bar : public IFoo
{
  public:
    virtual void foo() { ... };
    virtual void bar() { ... };
};

(I called the extracted class AFoo (A for abstract) rather than IFoo because it does not have to be a pure interface. Other methods that you don't want to override may stay as is in AFoo.)

This is not always as easy as shown, for example if you want to override a method that is 5 inheritance levels deep in the hierarchy. You will have to duplicate massive amounts of code, creatively use multiple inheritance, or rework your design to use more containment and less inheritance. But such is life of a C++ programmer who sells his soul to MISRA works in an industry that enforces coding standards.

一笔一画续写前缘 2025-02-21 01:32:20

我的答案的第一部分不会让您满意。 。 。

如果您的软件质量保证/功能安全/网络安全团队将在编码指南中定义此规则,则必须遵循它。

静态代码分析工具无论如何都会找到它,并将其报告为不合规。很可能会创建票。然后,您必须解决问题。

  • 要么修改代码
  • 进行专家同行代码审核,并给予理由,

因为需要此规则,它很可能会在您的静态代码分析工具的规则集中启用。

而且,如果您进行了第三方代码审核,将始终找到它,并且需要给出理由,为什么违反规则。

所有这些可能很烦人,但最好遵守规则。


接下来,理由。

规则说:

通过继承Hierachy

在每个路径上的每个虚拟函数的定义不得超过一个定义

该规则已制定,以便读者,尤其是代码审查阶段中的审阅者可以肯定的是可以从类Hierachy中的任何点执行的功能是明确的。它必须与读者的期望一致。

该规则还阻止了统治地位。请阅读这篇Wikipedia文章关于这一点。

因此,目标是立即“查看”哪些函数将被调用,而无需研究C ++语言定义中所有细微的智能内容。

规则中的示例代码比您上面显示的要多一点,提供了更好的示例和解释,因此有可能获得良好的理解。


规则10-3-2的解释是相似的:

使事情变得清晰。

如今,覆盖​​关键字也可以使用,但有不同的意图。如果您认为自己正在覆盖某些功能,但具有错别字或其他签名,它将导致编译错误。我建议也使用覆盖。


请再次注意:“虚拟”也不是强制性的。没有它们,代码将(很可能)起作用。

但是,如果您阅读代码,那么事情将会更加清楚。

因此,为了清楚起见。

The first part of my answer will not be satisfying for you . . .

If your software-quality-assurance/functional-safety/cyber-security team, will define this rule in the coding guidelines, then you must follow it.

The static code analysis tools will find it anyway and report it as non compliant. Most probably a ticket will be created. Then, you must resolve the issue.

  • Either modify the code
  • Make an expert peer code review and give a justification

Since this rule is required, it will most probably be enabled in the ruleset for your static code analysis tool.

And, if you have a 3rd party code audit, it will always be found, and you need to give a justification, why the rule is violated.

All this may be annoying, but better to follow the rules.


Next, the rationale.

The rule says:

There shall be no more than one definition of each virtual function on each path through the inheritance hierachy

The rule has been made, so that readers and especially reviewers in the code review phase can be sure that the version of the function that can be executed from any point in the class hierachy is unambiguous. It must be consistent with readers expectation.

And the rule also prevents call by dominance. Please read this Wikipedia article about that.

So, the goal is to immediately "see" which functions will be called, without the need to study all the nitty gritty stuff in the C++ language definition.

The example code in the rule, which is a little bit more than you are showing above, better examples and explanations are given, so that it is possible to get a good understanding.


The explanation for rule 10-3-2 is similar:

Make things clear.

The override keyword could also be used nowadays, but has a different intention. It will lead to a compile error, if you think that you are overwriting some function, but have a typo or different signature. I would recommend to use also override.


Please note again: Neither "virtual" nor "override" are mandatory. The code will (most probably) work without them.

But if you read the code, then things will be more clear.

So, it is for clarity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文