什么时候应该使用方法重载与不同命名的方法

发布于 2024-09-13 23:35:33 字数 476 浏览 3 评论 0原文

有时,我觉得方法重载可能会造成混乱。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

卷耳 2024-09-20 23:35:33

肯定是超载了。

好吧,所以调用哪个函数并不“明显”(有争议)……那又怎样?你并不关心它可以接受不同类型的参数,它只需要做它的事情。如果您根据不同的重载有不同的行为,那么您就滥用了重载,而不是指出了其中的缺陷。

滥用重载的示例:

// good:
struct has_properties
{
    void property1(float); // set property1, which happens to be a float
    void property2(int); // set property2, which happens to be an int
};

// bad:
struct has_properties
{
    void property(float); // set property1, abusing that it's a float
    void property(int); // set property2, abusing that it's an int
};

希望您在这里看到问题。如果两个函数具有相同的名称,则它们应该执行相同的操作。

更好的是,如果您只是想允许在不同类型上进行操作的可能性,只需使用模板即可。 (这可以说是一种重载形式。)

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:

// good:
struct has_properties
{
    void property1(float); // set property1, which happens to be a float
    void property2(int); // set property2, which happens to be an int
};

// bad:
struct has_properties
{
    void property(float); // set property1, abusing that it's a float
    void property(int); // set property2, abusing that it's an int
};

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.)

ぽ尐不点ル 2024-09-20 23:35:33

尽量避免使用多个同名的虚拟方法。或者您可能希望在派生类中重写它们。看下面的例子:

#include <string>

struct Base {
    virtual void foo(const std::string& arg) {
    }
    virtual void foo(int arg) {
    }
};

struct Derived : Base {
    // Only std::string version is overriden.
    virtual void foo(const std::string& arg) {
    }
};

int main() {
    Derived d;
    // This fails to compile because the name lookup stops
    // after Derived::foo has been found.
    d.foo(42); 
}

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:

#include <string>

struct Base {
    virtual void foo(const std::string& arg) {
    }
    virtual void foo(int arg) {
    }
};

struct Derived : Base {
    // Only std::string version is overriden.
    virtual void foo(const std::string& arg) {
    }
};

int main() {
    Derived d;
    // This fails to compile because the name lookup stops
    // after Derived::foo has been found.
    d.foo(42); 
}
小耗子 2024-09-20 23:35:33

如果您本质上对这两个函数执行相同的操作,它们只是类型参数不同,那么使用模板而不使用重载可能更有意义。

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.

你怎么这么可爱啊 2024-09-20 23:35:33

我会把它放在评论中,但我还不能。既然您将其标记为 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++.

邮友 2024-09-20 23:35:33

当然,在 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.

疧_╮線 2024-09-20 23:35:33

重载函数是对行为相似的函数进行分组的 C++ 方法。

do_it (A a, B b);
do_it (B b, int i = 0);

具有不同名称的函数(例如 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.

do_it (A a, B b);
do_it (B b, int i = 0);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文