Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shadowing a member function with a function argument

Tags:

c++

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?

like image 849
BAdhi Avatar asked Nov 23 '25 22:11

BAdhi


1 Answers

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.

like image 191
eerorika Avatar answered Nov 25 '25 13:11

eerorika