Xcode learns C + + (V, overloads of functions and extern "C")

Source: Internet
Author: User

One: Overloading of functions

What do you mean overloaded?

In the same scope, if the names of the 2 functions are the same, and the argument lists are different, they are called "overloads"

form of function overloading

Different shape parameter quantities

such as void Test (int a) and void test (int a, int b)

Different form parameter types

such as void Test (int a) and void Test (double A)

... ...

It is important to note that the following conditions do not belong to the overloaded

void Test (int a) and void test (int b) do not constitute overloads, they represent the same function

void Test (int a) and int test (int a) do not constitute overloading

Second: the implementation principle of function overloading

C + + supports function overloading because the compiler adapts the name of each function

Suppose that the prototype of a function is: void foo (int x, int y);

The function is compiled by the C compiler and is named _foo in the symbol library.

The C + + compiler produces names like _foo_int_int (different compilers may generate different names, but all use the same mechanism, resulting in a new name called "Mangled Name")

_foo_int_int Such a name contains the function name, function parameter number and type information, C + + is this mechanism to implement function overloading

That is, in C + +, the function void foo (int x, int y) is not the same as the symbol generated by the compilation of void foo (int x, float y), which is _foo_int_float.

III: extern "C"

The most important function of extern "C" is to implement c\c++ mixed programming, such as c\c++ function intermodulation

Note: extern must be followed by uppercase C

Variables and functions modified by extern "C" are compiled and concatenated in C language.

That is, the extern "C" void Test (int a, int b) is generated by the compiler function name is _test, no longer a name like _test_int_int

The following notation is equivalent:

extern "C" void Test (int a, int b);

extern "C" void test2 (int a, int b);

Equivalent to

extern "C"

{

void Test (int a, int b);

void Test2 (int a, int b);

}

Because the C language does not support the syntax of extern "C", the general C + + header file's function declaration is so rigorous:

#ifdef __cplusplus

extern "C"

{

#endif

void Test (int a, int b);

void Test2 (int a, int b);

#ifdef __cplusplus

}

#endif

Xcode learns C + + (V, overloads of functions and extern "C")

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.