Why was function overloading added to C++?

前端 未结 6 1892
囚心锁ツ
囚心锁ツ 2021-02-03 21:03

I have a C background. I was just wondering why was function overloading added to C++? C doesn\'t have function overloading but C++ does, what was the need for it?

What

6条回答
  •  猫巷女王i
    2021-02-03 21:51

    You could get the answer straight from the horse's mouth: The Design and Evolution of C++ by Bjarne Stroustrup devotes an entire chapter to overloading, its history, evolution, design tradeoffs and decisions.

    I won't recount the story here, but will mention a couple of interesting historical facts:

    • operator and function overloading are closely related;
    • in early C++ there used to be a special keyword (overload) that had to be used to declare an identifier as overloaded;
    • function overloading requires type-safe linking (i.e. name mangling); when first implemented, it helped discover a surprising number of link-time errors in existing C and C++ code (to quote Stroustrup, it was like "running lint on a C program for the first time -- somewhat embarrassing".)

提交回复
热议问题