8 – Two Functions Same Name

Something else I noticed interesting about methods or functions in C++. I actually found this one out by accident when I wrote two functions that had the same name. Let me see if I can find it. So I wrote two functions that were both called close enough, and one was meant to compare two floats. Chino saw for that like floating point they’re close but they’re not exactly equal, I wanted to return true, if two flips were close enough. And another was for two grids. And then want to return true if those were close enough to being equal, which just meant that all their actual values inside were close enough. And asked people to do this, I couldn’t believe this worked. But they were close enough once or took a two vector vector floater or grids, and another version or two floats. What’s going on here, why is this allowed? Well, the compiler doesn’t see those as the same thing. They have entirely different signatures. I mean if you sign something, I sign something, they would be different. And what it does is it takes these functions with the types that it takes. And it takes this close enough vector vector grid, and it makes it all part of the function name. You don’t see that. But if you looked inside the compiler, you would be able to see where it started to treat all of the functions, defined with the name, and all of their arguments. So this is something called overloading. It can be very useful if you want to have a function that is close enough on floats or close enough on integers. You don’t have to do everything specifically different. You have to have lots of different function names, you want it to know if it’s close enough. This is a feature of people. No. I felt it was nice to really do it that way.

%d 블로거가 이것을 좋아합니다: