Function overloading: nhiều hàm cùng tên, khác signature (số/kiểu tham số). Compiler chọn đúng bản tại compile-time.
Operator overloading: tái định nghĩa toán tử (+, <<, ==, ...) cho kiểu tự định nghĩa.
struct Vec2 {
float x, y;
Vec2 operator+(const Vec2& o) const {
return { x + o.x, y + o.y };
}
};
Vec2 a{1,2}, b{3,4};
Vec2 c = a + b; // gọi operator+Khác biệt với overriding: override là ghi đè hàm ở lớp con (runtime polymorphism), overloading là cùng tên khác signature (compile-time).
Function overloading: multiple functions with the same name but different signatures (parameter count/types). The compiler picks the right version at compile-time.
Operator overloading: redefine operators (+, <<, ==, ...) for user-defined types.
struct Vec2 {
float x, y;
Vec2 operator+(const Vec2& o) const {
return { x + o.x, y + o.y };
}
};
Vec2 a{1,2}, b{3,4};
Vec2 c = a + b; // calls operator+Distinct from overriding: override rewrites a base-class method in a subclass (runtime), overloading is same name + different signature (compile-time).