C + + function overload explanation and instance code _c language

Source: Internet
Author: User
Tags constant

Overloading of C + + functions

Defined

In the same scope, the function name is the same, the function's argument list differs between functions to form an overload relationship, and the function with the same name in different scopes follows the principle of hiding the identifiers

ATTENTION: Overloading has nothing to do with the return value type of a function, because declaring a function requires no return type, so it cannot be used to distinguish which function

An overload is formed between an ordinary function and a normal member function, and an overloaded constant object invokes a regular member function, and a generic object invokes a generic member function

Class a{...
  Public:
    void Getval () const{...}
    void Getval () {...}}
;
int main () {
  const a A;
  A b;
  A.getval (); Constant function
  b.getval ();//normal member function
}

Functions in different namespace can also be used in the same scope by the namespace instruction/namespace declaration, thus constituting the overloaded relation
Overloaded functions in different namespaces

#include <iosteam>
using namespace std;
namespace ns1{
  void fa (int i) {cout<< "in ns1:i=" <<I<<ENDL;}
namespace ns2{
  void Fa (double D) {cout<< "in ns2:i=" <<D<<ENDL;}
int main () {
  //Use namespace ns1 using the namespace instruction method
  ;
  using namespace ns2;
  FA (a);
  FA (3.14);
  return 0;
}

Overload matching principle

Full match > Constant conversion/constant matching > Upgrade conversion > Standard conversion > Custom conversion > Ellipsis matching
Note: In programming, as far as possible to achieve a complete match, if it can not exactly match, will generally be based on the above principles to choose

Overload principle

C + + compiler is to change the name of the function, the parameter list information in the underlying function name, so as to achieve the function of overloading, assembly function name in accordance with < scope + RETURN type + function name + parameter list > in the way embodied in the Assembly, Eg:_z4showii:

Thank you for reading, I hope to help you, thank you for your support for this site!

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.