Function Overloading for User-Defined Types

Learn about function overloading for user-defined types and the limitations of function overloading.

Function overloading for user-defined types

Function overloading is useful with structs and classes. Furthermore, overload resolution ambiguities are much less frequent with user-defined types. Let’s overload the info() function above for some of the types that we have defined in the structs chapter:

struct TimeOfDay {
    int hour;
    int minute; 
}
void info(TimeOfDay time) {
    writef("%02s:%02s", time.hour, time.minute); 
}

This overload enables TimeOfDay objects to be used with info(). As a result, variables of user-defined types can be displayed in exactly the same way as fundamental types:

Get hands-on with 1400+ tech skills courses.