C ++ collection-function Overloading

Source: Internet
Author: User

C ++ collection-function Overloading

C ++ collection-function Overloading

Preface

Function overloading does not seem to be difficult, but there are still some issues worth noting. The following describes several keywords:

Body 1. Scope

Several facts need to be pointed out about the scope

Areas enclosed in braces {} are in the same scope. Common areas include function bodies, for, and if statements. Variables with the same name cannot appear in the same scope. If a function has the same name, it is a function overload problem. Whether or not the same name is in different scopes does not affect. Regions outside of all functions are global scopes. First, you must note that, Function in the same scope will be overloaded.. Functions in different scopes, even with the same name, have the same parameter type and return value type. They are also two different functions.
2. Hide when the scope is nested.
# Include
 
  
Using namespace std; // varint var = 0 in the global scope; void print (void) {cout <"Global print" <endl;} int main () {// g_var in the local scope will hide g_varint var = 1 in the global scope; // local print will also hide global printauto print = [] (void) {cout <"local print" <endl ;}; // all the following calls are local cout <"var =" <var <endl; print (); // call the global cout <": var =" <: var <endl;: print (); cin. get (); return 0 ;}
 
Run kernel/K/kernel/nS1LX308O1xL7Nyse + 1rK/tcShowrS/rLYz9bP87Htw/kernel/K1xM2sw/Rack + Cgo8aDM + rack/Rack = "brush: java; "> # include 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"; print ("zhangxiang"); cout <endl; print (str); cin. get (); return 0 ;}Run

If you change void print (const char * str) to void print (char * const str), the following error occurs: error C2084: function "void print (char * const) after compilation) 'Already has a body, that is, the top-level const cannot distinguish between overload.

3. Overwrite or overwrite issues exist only for virtual functions. Overwriting is similar to hiding, but not overloading. Coverage is discussed in virtual functions.



The C ++ directory of this column picks up the CCPP Blog directory of all contents

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.