Chapter 8 advanced features of C ++ functions (8.1 concept of function overloading)

Source: Internet
Author: User

 

8.1 concept of function Overloading

1. What if a C ++ program needs to call a compiled C function?

Void Foo (int x, int y );

After the function is compiled by the C compiler, the name of the function in the library is _ Foo, while the C ++ compiler will generate something like _ foo_int_int

Such names are used to support function overloading and type-safe connections. C ++ programs cannot be compiled because their names are different.

Directly call the C function. C ++ provides a C connection to exchange specified symbols.Extern "C"To solve this problem.

For example:

Extern "C"

{

Void Foo (int x, int y );

Other // other functions

}

Or write it

Extern "C"

{

# Include "myheader. h"

Other // other C header files

}

This tells C ++ to compile the interpreter. The function foo is a C connection. You should find the name _ Foo in the library instead

_ Foo_int_int. The C ++ compiler developer has processed the header file of the C standard library as extern "C", so

We can use # include to directly reference these header files.

 

2. Note that not two functions with the same name can constitute an overload.

Overload (the number or type of parameters are different)

The name of the global function and the member function of the class is not counted.Overload because the function has different scopes.For example:

Void print (response); // global function

Class

{Signature

Void print (member); // member function

}

Whether the parameters of the two print functions are different, if a member function of the class calls a global function

Print. to be different from the member function print, the ':' flag should be added when the global function is called. For example

: Print (member); // indicates that print is a global function rather than a member function.

 

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.