Overload and namespace (learning notes)

Source: Internet
Author: User

Time: 2014.07.01

Location: Base

Bytes -----------------------------------------------------------------------------------------

The effect of namespaces on function matching is mainly manifested in the use of using declarations or using instructions to add certain functions to candidate function sets (different versions.

I. Search and reload related to real parameters

For a function that accepts real arguments of the class type, its name search is performed in the namespace where the class of the real argument is located. We will search for candidate functions in the namespace of the base classes of the real-name and real-name base classes. All functions in these namespaces with the same name as the called function will be added to the candidate set, even if some of these functions are invisible in the calling statement. For example:

namespace NS{   class Quote{/*......*/};   void Display(const Quote&){/*......*/}}
In the namespace NS, we define a quote class and a display function, which accepts parameters of the quote class type.

class BulkItem:public NS::Quote{  /*......*/};
Class bulkitem inherits the quote class under the namespace NS
int main(){  BulkItem book1;  Display(book1);  return 0;}
Here, the real parameter passed to display is a class type bulkitem, so the search for the candidate function that calls the statement will not only be found in the scope of the calling statement, that is, the main () function, it will also be searched in the namespace where bulkitem has its base class quote. That is, the function display (const quote &) declared in the namespace NS will also be added to the candidate function set. So that the display function can be correctly called here.

Bytes -----------------------------------------------------------------------------------------

2. Reload and using declarations the using declaration statement declares only a name, not a specific function. For example:
Using ns: Print (INT); // This will cause compilation errors, because using cannot declare the specific function using ns: Print; // correct. Using only declares one name here.
In this way, all versions of the function will be introduced into the current scope. The function introduced by the using declaration statement will reload other functions with the same name in the scope of the declaration statement (that is, as long as the conditions meet, the functions introduced by the using declaration statement and other functions with the same name in the scope of the declaration statement constitute an overload relationship ). For example, when the using statement declaration appears in a local scope, the introduced name will hide the declaration of the outer scope and reload the function with the same name in this scope. However, if there is already a function in the scope of the using declaration that has the same name as the newly introduced function and has the same name as the parameter list, the using declaration will cause an error. Bytes -----------------------------------------------------------------------------------------
3. overload and using indicate that using can promote the namespace members to the outer scope. If a namespace function has the same name as the function in the namespace's scope, then, the functions in the namespace are added to the reload set. For example:
Namespace my_space {extern void print (INT); extern void print (double) ;}// The following is a common function declaration void print (const STD: string &); // using indicates that it will promote the namespace name to the outer scope, and overload the function with different parameters with the same name as the outer scope, for example, here using indicates to add the name to the using namespace MySpace in the Candidate function set called by print; // The following candidate function versions are available when the print function is used: // print (INT) in MySpace) // print (double) in MySpace // print (const STD: string &) void foobar (INT ival) {print ("value:") explicitly declared in the current domain :"); // This statement calls the print (const string &) print (ival) of the global function; // calls the print (INT)} In The namespace )}
Note the difference between the using Declaration and the using indication: The using declaration only declares the name that exists in the namespace, introduce all function versions or name variables of the name in the namespace to the current scope. Using indicates that all the Members in the namespace are promoted to the outer scope. In addition, for the using indication, introducing a function with the same name as an existing function and the list of parameters does not produce errors. In this case, you only need to specify whether the version to be called is the version in the namespace or the version in the current scope. Bytes -----------------------------------------------------------------------------------------
4. Multiple using indications and overloading if multiple using indicators exist, the names from each namespace will become part of the candidate function set. For example:
Namespace spaceone {int print (INT);} namespace spacetwo {double print (double);} // use the using command to create an overload function set using namespace spaceone from different namespaces; using namespace spacetwo; // You can also define a long double print (Long Double) function with the same name in the current domain; int main () {print (1 ); // call print (INT) print (2.1) in spaceone; // call print (double) return 0 ;}
In the preceding global scope, the reload set of function print includes print (INT), print (double), and print (Long double), although their declarations are in different scopes, but they all belong to the candidate function set called by print in the mian 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.