Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why variables and functions cannot have same name?

Tags:

c++

I read an article explains why function and variable cannot have same name, most of those people says it's because if I try to get address of those things, compiler will not understand which I want, but in C++, overload function is valid, and if I try to get address of overloaded function, it will generate an error, so why not just generate an error while getting variable and function have same name, could anyone tell me why C++ standard not allowed it?


1 Answers

It doesn't need to generate an error when trying to get the address of variables and functions with the same name, because you cannot have variables and functions with the same name in the same scope.

If one is in a tighter scope, and hides the other, then there is no conflict, and no reason to produce an error.

Trying to take the address of an overloaded function without providing a resolution hint produces an error because there is literally nothing else the compiler can do; it doesn't know which function you're talking about.

like image 76
Asteroids With Wings Avatar answered Oct 30 '25 03:10

Asteroids With Wings



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!