C + + Supplements--function overloading

Source: Internet
Author: User

C + + Supplements--function overloading

Preface

Function overloading does not seem difficult, but there are still some issues worth noting. Here are some key words to start with:

Body1. Scope

There are several facts to be pointed out about scopes

    1. The area enclosed in curly braces {} is in the same scope, with a common function body, for, if statement, and so on.
    2. A variable with the same name cannot appear in the same scope, and if the function has the same name, it is the function overloading problem. The same name is not affected in different scopes.
    3. All areas outside of the function are global scopes.
first, it is important to point out that a function in the same scope has overloaded problems . Functions in different scopes are two different functions, even if they have the same name, and the parameter types and return value types are the same.
2. HideWhen a nested relationship exists in the scope, a hidden phenomenon occurs.
#include <iostream>using namespace std;//is located at the global scope of varint var = 0;void print (void) {cout << global print << E NDL;} int main () {///G_var at the local scope hides g_varint var = 1;//Local print in the global scope also hides global printauto print = [] (void) {cout << local print "<< endl;};/ /The following calls are all local cout << "var =" << var << endl;print ();//Call Global cout << ":: var =" <<:: Var << ; Endl::p rint (); Cin.get (); return 0;}
Run

because the main function body is nested inside the global scope, a hidden behavior occurs when an object with the same name appears. To invoke global objects, you use the global operator::. It needs to be further pointed out that hiding is just a phenomenon, essentially the object name lookup rule. The name lookup rules for C + + are found in this section from where the name appears to the beginning of the scope where the name is located. Find is no longer found, can not find to jump to the previous scope to continue to find. in this sample code, you can find the Var and print in the main function, so the call is local. shadowing indicates that a function of the same name, different scopes, is not an overloaded relationship.
3.const and Reloadthe const and overloaded connection is that overloads can be distinguished only through the underlying const.
#include <iostream>using namespace std;void print (const char *str) {cout << void print (const char *STR) call << endl;cout << str;} void print (char *str) {cout << void print (char *str) call << endl;cout << str;} int main () {char str[] = "David";p rint ("Zhangxiang"); cout << endl;print (str); Cin.get (); return 0;}
Run

If you change the void print (const char *str) to void print (char *const str), you are prompted with an error after compiling:the error c2084:function ' void print (char *const) ' already has a body, which means that the top-level const cannot differentiate the overloads.




directory of this column
    • C + + Supplements directory
Directory of all content
    • CCPP Blog Directory

C + + Supplements--function overloading

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.