为什么这两个函数不能重载?

时间:2016-06-18 12:32:34

标签: c++ overloading

为什么我不能重载这两个函数?

#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);

1 个答案:

答案 0 :(得分:2)

根据标准:

  

13.1可重载声明
....

     

(3.4) - 仅在const和/或volatile存在或不存在时有所不同的参数声明是   当量。也就是说,当忽略每个参数类型的constvolatile类型说明符时   确定正在声明,定义或调用哪个函数。 [实施例

   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的指针”和“指向volatile T的指针”是   考虑了不同的参数类型,“对T的引用”,“对const T的引用”和“引用   volatile T“。

相关问题