'How does the compiler not confuse local variable named size with function call size()?
While reading a C++ textbook, I came across this specific piece of code:
vector<double> homework;
typedef vector<double>::size_type vec_sz;
vec_sz size = homework.size();
I wonder how isn't there any conflict during compilation since the local variable size and the function being called size() share the same name and (here expecting a correction if I am wrong) they are apparently in the same scope.
If they aren't in the same scope, then how does the compiler handle these two names in a single statement?
Thanks for your time in advance I hope my question makes sense!
Solution 1:[1]
how isn't there any conflict during compilation since the local variable size
Because size in homework.size() is a qualified name. While the name size without any qualification is an unqualified name. When you wrote homework.size() only the class scope corresponding to the class name homework is searched. So there is no clash between the two names if you were to write:
//----vvvv-------------------->local variable named size
size = homework.size();
//--------------------^^^^---->data member named size in class scope
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
