Override and overload in C++

后端 未结 7 1496
生来不讨喜
生来不讨喜 2020-12-04 05:56

Yes, I do understand the difference between them. What I want to know is: why OVERRIDE a method? What is the good in doing it? In case of overload: the only advantage is you

7条回答
  •  半阙折子戏
    2020-12-04 06:24

    One use of overloading is for use in templates. In templates, you write code that can be used on different data types, and call it with different types. If functions that take different arguments had to be named differently, the code for different data types would in general have to be different, and templates just wouldn't work.

    While you may not be writing templates yet, you're almost certainly using some of them. Streams are templates, and so are vectors. Without overloading, and therefore without templates, you'd need to call Unicode streams something different from ASCII streams, and you'd have to use arrays and pointers instead of vectors.

提交回复
热议问题