为什么这两个功能不能超载



为什么我不能超载这两个功能?

#include <iostream>
using namespace std;
class Base
{
    public:
    void run(int a);
    void run(const int a);
};
int main() {
    // your code goes here
    return 0;
}

输出:

prog.cpp:8:7: error: 'void Base::run(int)' cannot be overloaded
  void run(const int a);
       ^
prog.cpp:7:7: error: with 'void Base::run(int)'
  void run(int a);

根据标准:

13.1超载声明
....

(3.4) - 仅在const和/或volatile存在或不存在的参数声明为 相等的。也就是说,当每种参数类型的constvolatile类型分类器被忽略 确定正在声明,定义或调用哪个函数。[&nbsp; em>示例:

   typedef const int cInt;
   int f (int);
   int f (const int); // redeclaration of f(int)
   int f (int) { /* ... */ } // definition of f(int)
   int f (cInt) { /* ... */ } // error: redefinition of f(int)

- 结束示例]

仅在参数类型规范的最外层级别的 constvolatile类型分配器 以这种方式被忽略;constvolatile埋在参数类型中的类型特征器 规范很重要,可用于区分超载函数声明。在 特别是,对于任何类型的T,"指向T的指针","指向const T的指针"one_answers"指向volatile T"是 被认为是不同的参数类型,以及"引用T","引用const T"one_answers"引用 volatile T。"

最新更新