☰
  • 首页
  • 规则分类
  • 项目介绍
search
•••

未访问 this 指针的成员函数应使用 static 声明

6.3.9 ID_this_notUsed
目录 › next › previous

如果未访问 this 指针的成员函数没有被设计为静态成员函数,很可能意味着错误或功能不完整。

示例:

class A {
    static int s;

public:
    static int bar() {   // Compliant
        return s--;
    }
    int foo() {          // Non-compliant, missing ‘static’
        return s++;
    }
    ....
};

例中 foo 函数只访问了静态数据成员,但在调用时仍会将 this 指针作为参数,这在逻辑上是矛盾的,所以应使用 static 关键字明确声明。

参考

MISRA C++ 2008 9-3-3
Copyright©2024 360 Security Technology Inc., Licensed under the Apache-2.0 license.