class A{
public:
void foo(int x)
{
cout << \"foo with one\\n\";
}
void foo(int x, int y=10)
{
I do NOT recommend this but you CAN define such ambiguous methods and use them through different interfaces. (The following works at least with gcc 4.8.0 using -std=c++11.)
Consider two interfaces:
class IInterface1
{
public:
virtual void funky(int i) = 0;
virtual void funky(int i, int j) = 0;
};
class IInterface2
{
public:
virtual void funky(int i, int j = 0) = 0;
};
IInterface1 has funky method overloaded twice for different arguments i.e. same method name but one takes single int while another takes two ints. Note in interface implementation, funky method will need to have two implementations (one for one argument and another for two arguments).
IInterface2 has single funky method that takes either one or two ints when invoked. If not provided explicitly, the second int is defaulted. Note in interface implementation, funky method will need to have only one implementation (and it always takes two arguments regardless of whether one or two were provided during invocation).
Class that implements both interfaces:
class Foo : public IInterface1, public IInterface2
{
public:
void funky(int i) override
{ printf(" funky(int i) -> funky(%d)\n", i); }
void funky(int i, int j = 0) override
{ printf(" funky(int i, int j = 0) -> funky(%d, %d)\n", i, j); }
void funky(int i, int j = 0, int k = 0)
{ printf(" funky(int i, int j = 0, int k = 0) -> funky(%d, %d, %d)\n", i, j, k); }
};
For illustration, Foo also adds third overloaded version of funky method, one that takes three arguments (one mandatory and two optional).
Foo can now be used as illustrated below. Instance of Foo, master, can be used either directly, or different clients can get access to different interfaces of master object.
Foo master;
IInterface1& client1 = master;
IInterface2& client2 = master;
// AMBIGUOUS: master.funky(1);
// AMBIGUOUS: master.funky(2,3);
puts("master.funky(4, 5, 6);");
master.funky(4, 5, 6);
puts("client1.funky(7);");
client1.funky(7);
puts("client1.funky(8, 9);");
client1.funky(8, 9);
puts("client2.funky(10);");
client2.funky(10);
puts("client2.funky(11, 12);");
client2.funky(11, 12);
This will produce the following output:
master.funky(4, 5, 6);
funky(int i, int j = 0, int k = 0) -> funky(4, 5, 6)
client1.funky(7);
funky(int i) -> funky(7)
client1.funky(8, 9);
funky(int i, int j = 0) -> funky(8, 9)
client2.funky(10);
funky(int i, int j = 0) -> funky(10, 0)
client2.funky(11, 12);
funky(int i, int j = 0) -> funky(11, 12)
In summary, class can have apparently conflicting overloaded versions of a method. Ambiguity MUST be resolved when the method is invoked (or else the code will not compile).
PS: Again, since the above approach breaks the KISS principle, I do not condone it.