Rule A5-5-1 (required, implementation, automated)
A pointer to member shall not access non-existent class members.
Rationale
Usage of a pointer-to-member expression leads to undefined behavior in the following cases: The pointer to member is a null pointer. The dynamic type of the object does not contain the member to which the called pointer to member refers.
Example
// $Id: A5-5-1.cpp 302200 2017-12-20 17:17:08Z michal.szczepankiewicz $
class A
{
public:
virtual ~A() = default;
};
class AA : public A
{
public:
virtual ~AA() = default;
virtual void foo() { }
using ptr = void (AA::*)();
};
class B
{
public:
static AA::ptr foo_ptr2;
};
AA::ptr B::foo_ptr2;
int main(void)
{
A* a = new A();
void (A::*foo_ptr1)() = static_cast<void(A::*)()>(&AA::foo);
(a->*foo_ptr1)(); // non-compliant
delete a;
AA* aa = new AA();
(aa->*B::foo_ptr2)(); // non-compliant, not explicitly initialized
delete aa;
return 0;
}
See also
SEI CERT C++ Coding Standard [10]: OOP55-CPP: Do not use pointer-tomember operators to access nonexistent members