BAdhi
BAdhi

Reputation: 510

Shadowing a member function with a function argument

Take the following class for an example

class A
{
    int m_c;
public:
    void B(int C);    
    void C();
};

This would give out the following warning if i compiled with the -Wshadow argument

memberFuncArg.cpp: In member function ‘void A::B(int)’:
memberFuncArg.cpp:12:16: warning: declaration of ‘C’ shadows a member of 'this' [-Wshadow]
 void A::B(int C)
                ^

What are the consequences of shadowing a member function with an argument to another member function like this?

Upvotes: 3

Views: 891

Answers (1)

eerorika
eerorika

Reputation: 238331

What are the consequences of shadowing a member function with an argument

The consequence is that a programmer who reads the code may be confused about which entity is being referred to by C. They may have become accustomed to the fact that C is a member function, and reasonably (but mistakenly) expect this to be the case within B as well.

The consequence is much worse when the argument is not of type int, but of some other type that can be invoked with same arguments as the member function. The confused programmer would then read or write C() and expect it to call the member function, but the behaviour would be different than expected.

Upvotes: 3

Related Questions