我如何在c++的一个类中实现两个同名的函数

How can I implement two functions with same name with in one class in C++?

本文关键字:实现 两个 函数 一个 c++      更新时间:2023-10-16

我有两个同名但参数不同的void函数。代码没有编译。我正在使用eclipse进行编译。

注意:这里我正在寻找覆盖函数。问题解决了。

如何在一个类中实现两个同名的函数C ?

c++有C以外的类。

使用方法/函数重载:

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

注意,除了参数和限定符的签名,其他都是一样的