AUTOSAR C++14 Rule A5-5-1
Description
Rule Definition
A pointer to member shall not access non-existent class members.
Rationale
You use pointer-to-member operators .* and ->* to
        refer to a non-static class member of the first operand object. For instance, in this code
        snippet, the call (obj.*ptrToMember)(); is equivalent to
          obj.f();. Both calls refer to member function f() of
        class
        myObj:
class myObj {
  public:
    void f();
};
void func() {
    myObj obj;
    void (myObj::*ptrToMember)() = &myObj::f;
    (obj.*ptrToMember)(); // Equivalent to obj.f();
}- The dynamic type of the first operand does not contain the member referred to by the second operand. 
- The second operand pointer is null. 
Polyspace Implementation
Polyspace® flags the use of pointer-to-member operators in these instances:
- You cast a pointer-to-member type to another pointer-to-member type which does not contain the class member pointed to. For instance, in this code snippet, pointer-to-member - ptrToMemberis obtained from- myObj::fbut is then upcast to- baseObj::*which does not contain a method- f.Polyspace does not flag the casting operation if the type being cast to inherits from the other type. For example, in the preceding code, if- class baseObj { public: virtual ~baseObj() = default; }; class myObj : public baseObj { public: void f(); }; void func() { baseObj* foo = new baseObj(); void (baseObj::*ptrToMember)() = static_cast<void (baseObj::*)()>(&myObj::f); //Noncompliant (foo->*ptrToMember)(); }- baseObjinherits from- myObj.
- The second operand of the pointer-to-member operator is a null pointer. For instance, in this code snippet, - ptrToMemberis declared but not initialized and defaults to a null pointer.- class baseObj { public: virtual ~baseObj() = default; }; static void (baseObj::*ptrToMember)(); //Not initialized void func() { baseObj* foo = new baseObj(); (foo->*ptrToMember)(); //Noncompliant }
Troubleshooting
If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
| Group: Expressions | 
| Category: Required, Automated | 
Version History
Introduced in R2022a