I searched a lot for something like delegation of member functions but I did not find something about it. Maybe I searched for the wrong words...
I try to do something like the following:
class Foo1 {
int bar(int a, int b = 1, bool c = false);
int bar(int a, bool c);
}
// Can I write it like:
class Foo2 {
int bar(int a, int b = 1, bool c = false);
// This line is my question:
int bar(int a, bool c) : bar(a, 1, c);
}
My compiler said that only constructors take init lists but I think I read something like the above somewhere. Are there any exceptions from the rule that only constructors take init lists?
An init-list is for initializing the bases and members of a class object, which only makes sense for a class constructor. A function has nothing to initialize (except for the function parameters, which are automatically initialized from the arguments passed at each call).
But to defer one function to another is easy: you just call the other function in the first function's body.
class Foo2 {
int bar(int a, int b = 1, bool c = false);
int bar(int a, bool c) { return bar(a, 1, c); }
};