07-04-2017, 07:23 AM
Maybe this extended and commented sample answers your question:
Code:
#include
namespace nMISRA
{
class C {
public:
C ( int32_t &b_ ) : b( b_ ) { }
int32_t *getB () const
{
return &b; // Non-compliant
}
int32_t getState(void) const{return b;}
private:
int32_t &b;
};
}
int32_t main(void)
{
int32_t ret = 0;
int32_t state = 0;
nMISRA::C c(state);
*c.getB() = 1;// const member changing the state of the object, triggering error condition below
if (c.getState() != 0)
{
ret = -1;// error
}
return ret;
}
<t></t>