Skip to content
  • Overloading, Default Arguments & inline · C++

Overloading, Default Arguments & inline

LearnViaNotes @learnvianotes· 3 min read

Several functions can share a name if their parameter lists differ; the compiler picks the best match at compile time. Default arguments fill in trailing parameters.

Function overloading

C++
#include <iostream>
#include <string>

int area(int side)               { return side * side; }
int area(int w, int h)           { return w * h; }
double area(double radius)       { return 3.14159 * radius * radius; }
std::string describe(int)        { return "int"; }
std::string describe(long)       { return "long"; }
std::string describe(const std::string&) { return "string"; }

int main() {
    std::cout << area(4) << ' ' << area(2, 3) << ' ' << area(1.0) << '\n';
    std::cout << describe(1) << ' ' << describe(1L) << ' ' << describe("hi") << '\n';
    std::cout << describe('c') << '\n';   // char → int is a promotion: best match
}
Output
16 6 3.14159
int long string
int
RankConversion neededExample
1. Exact matchNone (or trivial: array→pointer, adding const)f(int) for f(5)
2. Promotionchar/short/bool → int, float → doublef(int) for f('c')
3. Standard conversionint → double, double → int, derived* → base*f(double) for f(5)
4. User-definedConstructor or conversion operatorf(std::string) for f("hi")
C++
void f(int) {}
void f(double) {}
int main() {
    f(5L);      // error: call to 'f' is ambiguous (long → int and long → double rank the same)
}

Default arguments

C++
#include <iostream>
#include <string>

void log(const std::string& msg, int level = 1, char end = '\n') {
    std::cout << "[" << level << "] " << msg << end;
}

int main() {
    log("start");
    log("warn", 2);
    log("inline", 3, ' ');
    log("done");
}
Output
[1] start
[2] warn
[3] inline [1] done
  • Defaults must be the trailing parameters: void f(int a = 1, int b); is illegal.

  • Specify them once — in the declaration (header), not again in the definition.

  • They're substituted at the call site at compile time; for virtual functions the default comes from the static type — a classic trap.

inline

inline originally meant “please paste the body at the call site”. Today compilers decide inlining themselves; the keyword's real meaning is ODR: an inline function (or C++17 inline variable) may be defined in a header included by many .cpp files without “multiple definition” link errors.

C++
#include <iostream>
inline int maxOf(int a, int b) { return a > b ? a : b; }   // OK to live in a header
inline constexpr int kVersion = 3;                           // C++17 inline variable
struct Box {
    int w = 2;
    int width() const { return w; }   // member functions defined in the class are implicitly inline
};
int main() { std::cout << maxOf(3, 9) << ' ' << kVersion << ' ' << Box{}.width() << '\n'; }
Output
9 3 2

Can overloaded functions differ only by const on a by-value parameter?

What's the difference between a macro and an inline function?

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report