为什么评论参数名称而不是保持原样 [英] Why comment parameter names rather than leave it as it is

查看:113
本文介绍了为什么评论参数名称而不是保持原样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时我看到这样的代码:

Sometimes I see code like this:

LRESULT OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)

推荐答案

我看到这样做的一个原因是你明确想告诉其他程序员不要使用参数,但将其留在注释中以描述其意图。

One reason I see for doing this is that you explicitly want to tell other programmers not to use the parameters, but leave them in comments for a description of their intent. I know this doesn't make sense now, but read on.

我会使用不同的例子:

class A
{
public:
   virtual void foo(int someProperty);
};

class B : public A
{
public:
   virtual void foo(int /*someProperty*/);
};

假设你想要一个特定的情况,你想要 B :: foo ()做一些额外的事情,然后用参数 0 A :: foo() c $ c>。你必须保持相同的函数签名,所以多态性工作,但是,在 B :: foo(),你实际上没有使用参数。也不想在未来使用它。它基本上是一个意向声明,说这个方法的逻辑不应该依赖于someProperty

Say you want this for a specific case where you want B::foo() to do some extra stuff, and then call A::foo() with the parameter 0. You have to keep the same function signature, so polymorphism works, but, inside B::foo(), you're not actually using the parameter. Nor do you want to use it in the future. It's basically a statement of intent, saying "the logic of this method should not depend on someProperty".

B::foo(int/*someProperty*/)
{
    //do some stuff
    A::foo(0);
}

使用参数名注释掉,你不能真正使用它您会遇到一些黑客)。但是注释的名字告诉你关于传递给 A :: foo()的参数的一些信息 - 它的someProperty从 A

With the parameter name commented out, you can't really use it (unless you get down to some hacking). But the commented name tells you something about the parameter you pass to A::foo() - its 'someProperty' from A.

现在,我不同意语法,但这可能是一个可能的解释。

Now, I don't agree with the syntax, but this can be a possible explanation.

这篇关于为什么评论参数名称而不是保持原样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆