Skip to main content

IAR Embedded Workbench for RISC-V 3.40

CONST-member-ret (C++ only)

In this section:
Synopsis

A member function qualified as const returns a pointer member variable.

Enabled by default

Yes

Severity/Certainty

Medium/Medium

mediummedium.png
Full description

A member function qualified as const returns a pointer member variable. This might violate the semantics of the function's const qualification, as the data at that address might be overwritten, or the memory itself might be freed. This will not be identified by a compiler, because the pointer being returned is a copy even though the memory to which it refers is vulnerable. This check is identical to MISRAC++2008-9-3-1.

Coding standards
MISRA C++ 2008 9-3-1

(Required) const member functions shall not return non-const pointers or references to class-data.

Code examples

The following code example fails the check and will give a warning:

class C{
  int* foo() const {
    return p;
  }
  int* p;
};

The following code example passes the check and will not give a warning about this issue:

class C{
  int* foo() {
    return p;
  }
  int* p;
};