什么时候应该使用方法重载与不同命名的方法
有时,我觉得方法重载可能会造成混乱。
class a {
public:
void copy(float f);
void copy(double d);
};
a me;
me.copy(1.2); // Not obvious at the first sight on which version we are calling.
对此的解决方法是。
class a {
public:
void copyFloat(float f);
void copyDouble(double d);
};
然而,使用不同名称的方法来执行相同的功能似乎也不是一个好主意。我想知道,您会考虑选择方法重载还是不同命名的方法?
Sometimes, I felt method overloading may create confusion.
class a {
public:
void copy(float f);
void copy(double d);
};
a me;
me.copy(1.2); // Not obvious at the first sight on which version we are calling.
A workaround on this is.
class a {
public:
void copyFloat(float f);
void copyDouble(double d);
};
However, having method with different name, to perform same functionality doesn't seem a good idea as well. May I know, what do you consider, to choose among method overloading, or method with different naming?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
肯定是超载了。
好吧,所以调用哪个函数并不“明显”(有争议)……那又怎样?你并不关心它可以接受不同类型的参数,它只需要做它的事情。如果您根据不同的重载有不同的行为,那么您就滥用了重载,而不是指出了其中的缺陷。
滥用重载的示例:
希望您在这里看到问题。如果两个函数具有相同的名称,则它们应该执行相同的操作。
更好的是,如果您只是想允许在不同类型上进行操作的可能性,只需使用模板即可。 (这可以说是一种重载形式。)
Overloading for sure.
Okay, so it's not "obvious" which function gets called (arguable)...so what? You don't care that it can take different types of parameters, it just needs to do its thing. If you have different behavior based on different overloads, you've abused overloads, not pointed out a flaw in them.
An example of abusing overloads:
Hopefully you see the problem here. If two functions have the same name, they should do the same thing.
Even better, if you're merely trying to allow the possibility for operating on different types, just use a template. (This arguably is a form of overloading.)
尽量避免使用多个同名的虚拟方法。或者您可能希望在派生类中重写它们。看下面的例子:
Try to avoid using multiple virtual methods with the same name. Or you will probably want to override them all in derived classes. Look at the following example:
如果您本质上对这两个函数执行相同的操作,它们只是类型参数不同,那么使用模板而不使用重载可能更有意义。
If you're essentially doing the same thing with the two functions, they just differ in their type arguments, then it might make more sense to use templates and not use overloading at all.
我会把它放在评论中,但我还不能。既然您将其标记为 C++,我想我应该告诉您,方法在 C/C++ 中通常称为函数。
I would put this in a comment, but I can't yet. Since you tagged this as C++, I thought I should tell you that methods are usually called functions in C/C++.
当然,在 float 与 double 的情况下,您不应该超载,因为它太容易出错。我听到人们争论说,在这种情况下,您应该始终使用不同的名称,以便完全明确并使读者尽可能清楚地了解事情,我倾向于同意。
Certainly in the case of float vs. double you should not overload, it's just too error prone. I've heard people argue that you should always use different names in cases like these just to be completely explicit and make things as clear as possible to the reader and I tend to agree.
重载函数是对行为相似的函数进行分组的 C++ 方法。
具有不同名称的函数(例如 do_it_with_a 或 do_it_with_a_b)是对行为相似的函数进行分组的 C 方法。
因此,如果您的函数的行为不同,请不要重载它们。
Overloaded functions is the C++ approach to grouping functions which are similar on their behavior.
Functions with different names (e.g. do_it_with_a or do_it_with_a_b) is the C approach to grouping functions which are similar on their behavior.
So if your functions are different in their behavior, don't overload them.