Default C ++ function overloading and parameters

First, function overloading

1.1 origin overloaded

Natural language, a word can have many different meanings that the word is overloaded. It can be determined by the context of the word in the end what kind of meaning. "Words overload" can make the language more concise. For example, "eat" the meaning is very broad, it is not necessary to make it clear what each have to eat non-specific. Do not pedantic like a hole has been, to say the fennel beans flavored with aniseed There are four written word.

In the C ++ program, similar semantics, the functions of several functions can be represented with the same name, i.e., overloading. It is easy to remember, improved ease of use function, which is a reason for C ++ language using heavy-duty mechanism. E.g. eatBeef function in the embodiment, eatFish, eatChicken can use the same function name represents Eat, distinguished by different types of parameters.

// 重载函数 Eat
void eatBeef(…); // 可以改为 void eat(Beef …);
void eatFish(…); // 可以改为 void eat(Fish …);
void eatChicken(…); // 可以改为 void eat(Chicken …);

Another reason for using C ++ language is overloading: class constructor should override mechanism. Because C ++ class with the same name as the provisions of the constructor, the constructor only one name. If you want to create an object using several different methods how to do? No choice but to use heavy-duty mechanisms. Therefore constructor class can have a plurality of the same name.


1.2 Overload is how to achieve?

Several overloaded functions the same name is still different functions, how they distinguish it?

We naturally think of two elements function interface: parameters and return values. If the same name as a function of different parameters (including the type, in a different order), so that they are easy to distinguish different functions.


If the function of the same name only return values ​​of different types, and sometimes can be distinguished, and sometimes not. E.g:

void function(void);
int function (void);

The above-described two functions, the first non-return value, the return value of the second type int. If so call the
function:

int x = function ();

Judgment can function is the second function. The problem is that in C ++ / C program, we can ignore the return value of the function. In this case, the programmer and the compiler does not know which function is called.

We can only rely on parameters not return a different value types to distinguish overloaded functions. Compiler generates different internal identifier for each overloaded function according to the parameters. Such as a compiler in the embodiment is generated as a function of three eat _eat_beef, _eat_fish, _eat_chicken internal identifier or the like (different compiler may produce different styles of internal identifier) is.


If a C ++ program you want to call C functions have been compiled after, how to do?

Suppose a C function declarations are as follows:

void foo(int x, int y);

The function is the C compiler in the library named _foo, and the C ++ compiler will generate names like _foo_int_int like to support function overloading and type-safe connection. Due to the different names compiled, C ++ program can not directly call C functions. C ++ provides a C connection to exchange specified symbol extern "C" to solve this problem. E.g:

extern “C”
{
    void foo(int x, int y);
    … // 其它函数
}

Or write:

extern “C”
{
#include “myheader.h”
… // 其它 C 头文件
}

This tells the C ++ compiler translation, a C function foo is connected, should go to the library to find the name _foo rather than looking _foo_int_int. C ++ compiler developers have been extern "C" handling of the C standard library header files, so we can reference these header files directly #include.


Note that the two functions are not the same name can constitute overloaded. Member function of the same name global functions and classes is not overloaded, because of the different scope of a function. E.g:

void print(…); // 全局函数

class A
{
    …
    void print(…); // 成员函数
}

Regardless of whether the print function parameter two different, if a class member function to call a global function print, in order to distinguish the members of the print function, global function is called when adding '::' flag. E.g:

::Print(…); // 表示 Print 是全局函数而非成员函数


Beware implicit type conversions 1.3

In the following example, the first parameter is a function of type int output, the second parameter is the output function of type float. Since the digital type itself does not automatically convert the type (referred to as an implicit type conversion) as a parameter when the number. Output statement (0.5) to a compilation error, because the compiler does not know which is converted to an int 0.5 float type parameter. Implicit type conversion in many places can simplify the process of writing, but may also stay hidden.

# include <iostream.h>

void output( int x)
{
    cout << " output int " << x << endl ;
}

void output( float x)
{
    cout << " output float " << x << endl ;
}

void main(void)
{
    int x = 1;
    float y = 1.0;
    
    output(x); // output int 1
    output(y); // output float 1
    output(1); // output int 1
    // output(0.5); // error! ambiguous call, 因为自动类型转换
    output(int(0.5)); // output int 0
    output(float(0.5)); // output float 0.5
}


Second, the default value of the parameter

There are some values ​​of the parameters at the same time each function call, writing such a statement will make people sick. C ++ language using default parameters the writing becomes simple (at compile time, a default value is automatically inserted by the compiler).

Use the default value of rule parameters:

[Rule 2-1] parameter default values can only appear in the function's declaration, but can not appear in the definition of the body.

void Foo(int x=0, int y=0); // 正确,缺省值出现在函数的声明中

void Foo(int x=0, int y=0) // 错误,缺省值出现在函数的定义体中
{
    …
}

Why is this so? I think there are two reasons: First, to achieve the function (defined) if there is already a default value independent of the parameters, so there is no need for default values appear in the definition of body function. Second, the default values of the parameters may change, modify the declaration of the function is clearly more convenient than to modify the definition of the function.


[2-2] rule if the function has multiple parameters, default parameters can only children one by one from back to front, otherwise it will lead to a function call statement grotesque.

void Foo(int x, int y=0, int z=0); // 正确
void Foo(int x=0, int y, int z=0); // 错误


Note that the use of default values ​​for parameters not given new performance function, just some of the writing becomes simple. It may improve ease of function, but also may reduce the intelligibility function. So we can properly use the default value of the parameter, to prevent the improper use of a negative effect.

#include <iostream.h>

void output( int x);
void output( int x, float y=0.0); // 参数缺省值只能出现在函数的声明中

void main(void)
{
    int x=1;
    float y=0.5;
    // output(x); // error! ambiguous call
    output(x,y); // output int 1 and float 0.5
}

void output( int x)
{
    cout << " output int " << x << endl ;
}

void output( int x, float y)
{
    cout << " output int " << x << " and float " << y << endl ;
}

In the above example, inappropriate use default parameters will result in generating output overloads ambiguity.


reference:

Advanced features "high-quality C ++ C Programming Guide Lam" Chapter 8 C ++ function


Guess you like

Origin www.cnblogs.com/linuxAndMcu/p/11309893.html