Can a function parameter hide another functions identifier?

173 Views Asked by At

Static code analysis identifies the constructor parameters begin and end as names that hide the corresponding member functions (read functions) according to Misra Rule 2-10-2 (Identifiers declared in an inner scope shall not hide an identifier declared in an outer scope). Is this correct?

template <typename T>
class TLineSegment
{
public:
   TLineSegment(const point_type& begin, const point_type& end)
      : m_begin(begin)
      , m_end(end)
   {
   }

   const point_type& begin() const
   {
      return m_begin;
   }

   const point_type& end() const
   {
      return m_end;
   }

private:
   point_type m_begin;
   point_type m_end;
}
2

There are 2 best solutions below

0
On

The compiler looks first at names, and then figures out what to do with them. So begin is the name of the function argument. The compiler won’t look past that to see if there’s some other definition that can be used in the function call.

0
On

In my understanding, this MISRA rule is about code readability and avoiding confusion rather then having code broken.

So from syntactical point of view, your program is correct, but from the MISRA point of view, it violates this rule, because it overwrites already existing identifier.

If put to extreme, some programmer not paying much attention might be confused by using the same names.

But from practical view, this is really a detail and in my opinion may be left as you have it. But if your company forces MISRA rules, you should fix it.