I am learning C++ using the books listed here. In particular, i read about overloading. So after reading i am trying out different examples to clear my concept further. One such example whose output i am unable to understand is given below:
int Name = 0;
class Name
{
int x[2];
};
void func()
{
std::cout << sizeof(Name) << std::endl; //My question is: Why here Name refers to the integer variable Name and not class named Name
}
int main()
{
func();
}
When i call func
, then in the statement std::cout << sizeof(Name) << std::endl;
, Name
refers to the int
Name
and not class named Name
. Why is this so? I expected that this would give me ambiguity error because there are two(different) entities with the same name. But the program works without any error and sizeof
is applied to int Name
instead of class named Name
. I have read about function overloading in the books. But not about this kind of overloading. What is it called and what is happening here.
PS: I know that i can write std::cout<< sizeof(class Name);
where sizeof
will applied to the class named Name
instead of variable int Name
. Also, the example is for academic purposes. I know that use of global variable should be avoided. The question is not about how to solve this(say by not using same name for those two entities) but about what is happening.