High quality C++/C Programming Guide-8th Chapter-c++ function Advanced Features (1)

Source: Internet
Author: User

In contrast to C-language functions, C + + adds four new mechanisms for overloading (overloaded), inline (inline), const, and virtual. The overloaded and inline mechanisms are available for both global functions and member functions of classes, and the const and virtual mechanisms are used only for member functions of the class. Overloading and inline are definitely good for the C + + language, but they can't be abused as a free lunch. This chapter explores the advantages and limitations of overloading and inline, explaining what should and should not be used and being wary of errors.

8.1 Concept of function overloading
The origin of 8.1.1 overload

In natural language, a word can have many different meanings, that is, the word is overloaded. One can judge the meaning of a word by context. The word overload can make the language more concise. For example, the meaning of "eating" is so extensive that it is not necessary to say exactly what to eat every time. Don't be pedantic like a hole already, said anise beans anise word has four kinds of writing.

In C + + programs, several functions that are semantically and functionally similar can be represented by the same name, that is, the function overload. This is easy to remember, improve the usability of the function, this is the C + + language to use the overload mechanism of a reason. For example, the function Eatbeef,eatfish,eatchicken in the example 8-1-1 can be represented using the same function name eat, distinguished by different types of arguments.

void Eatbeef (...); Can be changed to void Eat (Beef ...);

void Eatfish (...); Can be changed to void Eat (Fish ...);

void Eatchicken (...); Can be changed to void Eat (chicken ...);

Example 8-1-1 overloaded functions Eat

Another reason why the C + + language uses overloaded mechanisms is that class constructors require overloaded mechanisms. Because C + + requires constructors to have the same name as a class (see chap. 9th), a constructor can have only one. What if you want to create an object in several different ways? There is no choice but to use overload mechanism to implement. So a class can have multiple constructors with the same name.

How is the 8.1.2 overload implemented?

Several overloaded functions with the same name are still different functions, how are they differentiated? We naturally think of two elements of a function interface: parameters and return values.

If the parameters of a function of the same name are different, including the type, and the order is different, it is easy to distinguish them from different functions.

If a function of the same name is simply a return value type, it can sometimes be distinguished, sometimes not. For example:

void Function (void);

int Function (void);

The above two functions, the first one has no return value, and the second return value is the int type. If you call a function like this:

int x = Function ();

You can tell that the function is the second one. The problem is that in the C++/C program, we can ignore the return value of the function. In this case, neither the compiler nor the programmer knows which function is invoked.

Therefore, you can only rely on parameters rather than the difference of the return value type to distinguish overloaded functions. The compiler produces a different internal identifier for each overloaded function based on the parameters. For example, the compiler generates internal identifiers such as _eat_beef, _eat_fish, and _eat_chicken for the three eat functions in the example 8-1-1 (different compilers may produce different styles of internal identifiers).

What if C + + programs want to invoke the C function that has been compiled?

Suppose the declaration of a C function is as follows:

void foo (int x, int y);

The function is compiled by the C compiler in the library with the name _foo, and the C + + compiler produces names such as _foo_int_int to support function overloading and type-safe connections. Because the compiled name is different, C + + programs cannot call the C function directly. C + + provides an "C" connection Exchange specified symbol extern "C." to solve this problem. For example:

extern "C"

{

void foo (int x, int y);

...//other functions

}

or write

extern "C"

{

#include "Myheader.h"

...//other C-header documents

}

This tells C + + compiler, function foo is a C connection, should go to the library to find the name _foo instead of looking for _foo_int_int. C + + compiler developers have already made an extern "C" handle to the header files of the C. Standard library, so we can use #include to refer to these headers directly.

Note that not two functions have the same name that can form an overload. Global functions and the member functions of a class do not have the same name as overloads, because the scope of the function is different. For example:

void Print (...); Global functions

Class A

{...

void Print (...); member functions

}

Regardless of the parameters of the two print functions, if a member function of a class calls global function print, the global function should be called with the ':: ' flag in order to distinguish it from the member function print. Such as

::P rint (...); Indicates that print is a global function rather than a member function

Related Article

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.