如何在C ++中的一个类中实现两个具有相同名称的函数?

时间:2013-05-24 20:39:03

标签: c++ eclipse

我有两个具有相同名称但参数不同的void函数。而且,Code没有编译。我正在使用eclipse进行编译。

注意:我在这里寻找覆盖功能。问题解决了。

1 个答案:

答案 0 :(得分:3)

  

如何在一个类中实现两个具有相同名称的函数   C 1

C ++的课程不是C。

使用method/function overloading

class MyClass
{
   void method1(int x) {}
   void method1(int x, int y) {}

   void method2(int x) const {}
   void method2(int x) {}

   void method3(int x) {};
   void method3(int x, int y = 1) {}; // ERROR ambiguity! when .method3(1)
};

注意,一切都是相同的,但参数或限定符的签名。