• C++函数在重载时如何判断函数签名(function signature)是否相同


    Argument Type Differences

    参数类型区别

    (以下根据MSDN整理而成)
    Overloaded functions differentiate between argument types that take different initializers. Therefore, an argument of a given type and a reference to that type are considered the same for the purposes of overloading. They are considered the same because they take the same initializers. For example, max( double, double ) is considered the same as max( double &, double & ). Declaring two such functions causes an error.
    重载函数根据不同的参数初始化方式区分函数。因此,某个类型的参数和这个类型的引用参数被认为一样的,因为他们的初始化方式一致。所以,max( double, double ) 被认为和max( double &, double & ).一样。

    For the same reason, function arguments of a type modified by const or volatile are not treated differently than the base type for the purposes of overloading.
    同样原因,某个类型的参数和这个类型的const参数也认为是一样的

    However, the function overloading mechanism can distinguish between references that are qualified by const and volatile and references to the base type. This makes code such as the following possible:
    不过,函数重载机制可以区分引用和const、volatile引用。因此,下面代码可行。

    // argument_type_differences.cpp
    // compile with: /EHsc /W3
    // C4521 expected
    #include <iostream>
    
    using namespace std;
    class Over {
    public:
       Over() { cout << "Over default constructor\n"; }
       Over( Over &o ) { cout << "Over&\n"; }
       Over( const Over &co ) { cout << "const Over&\n"; }
       Over( volatile Over &vo ) { cout << "volatile Over&\n"; }
    };
    
    int main() {
       Over o1;            // Calls default constructor.
       Over o2( o1 );      // Calls Over( Over& ).
       const Over o3;      // Calls default constructor.
       Over o4( o3 );      // Calls Over( const Over& ).
       volatile Over o5;   // Calls default constructor.
       Over o6( o5 );      // Calls Over( volatile Over& ).
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    Pointers to const and volatile objects are also considered different from pointers to the base type for the purposes of overloading.
    另外,指针和const、volatile指针也被认为是不一样的
    总结一句话,重载时根据函数参数数量、类型进行区分,当const、volatile限定引用和指针类型的参数时,也可做区分的依据

  • 相关阅读:
    Shell编程之免交互
    webpack5基础--02_基本配置( 5 大核心概念)
    道可云元宇宙每日资讯|广东:打造人工智能、新型储能等新增长点
    Vue3源码【一】—— ref&reactive响应式原理及简单实现
    汇编语言之栈
    算法——多数相和
    LeetCode 1235. 规划兼职工作
    LeetCode算法心得——打家劫舍(记忆化搜索)
    物联网开发笔记(2)- 使用Wokwi仿真树莓派Pico点亮LED灯代码分析
    T、Class<T>以及Class<?>的理解
  • 原文地址:https://blog.csdn.net/mary288267/article/details/124908900