An interesting example of a function hidden in C + +
Source: Internet
Author: User
Function shadowing is the definition or declaration of a function that causes some other functions to be invisible. Function A hides the condition of function B: 1. Two functions have the same function name 2. has a different scope. A location that defines or declares a domain function, such as global scope, file scope, class scope, and so on. It should be noted that function shadowing can be caused by a declaration, not necessarily by a definition. This is illustrated by the interesting use examples that follow.
some of the situations where function hiding occurs 1. Global scope and file scope concealment2. Global scope and local scope3. File scope and local scope personally think that hiding is easy to cause some deep hidden problems, and difficult to understand and debug. You might want to avoid function concealment. here is the example. (This example is just an example of a demonstration of function overloading and shadowing. Only functional and said there is reason to believe that there is a better implementation)#include <iostream>
using namespace Std;
#define Use_long
static void method (int a)
{
cout<< "in (int)" <<endl;
}
static void method (Long a)
{
cout<< "in" (long) <<endl;
}
int main (int argc, char *argv[])
{
#ifdef Use_int
void method (int);
#endif//Use_int
When the macro use_int is defined, the execution result is: In method (int); When the macro Use_long is defined, the execution result is: In method (long);
In this example, if use_int is defined, void method (INT) is declared in the main function body. If the declaration is Use_long, void method (LONG) is declared in the main function body. When a function body is declared, the function has a local scope, and another function is overwritten. If you define Use_int and Use_long at the same time, guess what will be the result. The answer is in (int); The reason is that at this time two functions have a local scope, forming a function overload.
Also, if you have a certain understanding of JavaScript's function call chain. The hidden function is a piece of cake.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service