Question about an example from rule 9-3-1 - Printable Version +- MISRA Discussion Forums (https://forum.misra.org.uk) +-- Forum: MISRA C++ (https://forum.misra.org.uk/forumdisplay.php?fid=18) +--- Forum: MISRA C++:2008 rules (https://forum.misra.org.uk/forumdisplay.php?fid=19) +---- Forum: 6.9 Classes (C++) (https://forum.misra.org.uk/forumdisplay.php?fid=138) +---- Thread: Question about an example from rule 9-3-1 (/showthread.php?tid=1334) |
Question about an example from rule 9-3-1 - cxlin - 07-04-2017 Hi MISRA, I have a question about the following example from rule 9-3-1: Code: class C { Re: Question about an example from rule 9-3-1 - dg1980 - 07-04-2017 Maybe this extended and commented sample answers your question: Code: #include Re: Question about an example from rule 9-3-1 - cxlin - 07-04-2017 dg1980 Wrote:Maybe this extended and commented sample answers your question: Well, `state` is not part of the object `c` (and likely not "class data"). It doesn't make much sense if this rule is to enforce that `getB` only return a const handler of `state`, because `state` is already in scope and could be modified without calling `getB`! Code: int32_t state = 0; So I don't think the original example follows the rationale of the rule and thus the original question. Re: Question about an example from rule 9-3-1 - cxlin - 07-04-2017 dg1980 Wrote:Maybe this extended and commented sample answers your question: Well, `state` is not part of the object `c` (and likely not "class data"). It doesn't make much sense if this rule is to enforce that `getB` only return a const handler of `state`, because `state` is already in scope and could be modified without calling `getB`! Code: int32_t state = 0; So I don't think the original example follows the rationale of the rule and thus the original question. Re: Question about an example from rule 9-3-1 - dg1980 - 07-04-2017 As soon as you store a reference, it becomes class data. I slightly reworked the example, to make it even more obvious what evil code you could write, if this was not treated a violation of 9-3-1: Code: #include Re: Question about an example from rule 9-3-1 - cxlin - 07-04-2017 dg1980 Wrote:As soon as you store a reference, it becomes class data. To me that's not clear from the definition of "class data" (as mentioned in the original post). And this is exactly the question I'm asking MISRA to confirm. The same goes with a resource that's not allocated by the constructor or released by the destructor, but pointed to by a pointer member. Note the definition of "class data" actually made clear that static members are not "class data", and in the example the resource reference by `b` could be shared by multiple objects and acts basically the same as a static member. Quote:I slightly reworked the example, to make it even more obvious what evil code you could write, if this was not treated a violation of 9-3-1: The evilness of your code does not come from not following 9-3-1, but from the fact that a member referencing a shared object (with automatic storage). Removing `const` from `getB` and some other functions will make the code 9-3-1 compliant (under your interpretation) but doesn't really make it less evil. Re: Question about an example from rule 9-3-1 - misra cpp - 10-07-2017 The code doesn't violate the rule, as the value returned by the function does not allow any class member to be modified |