Why comment parameter names rather than leave it as it is

前端 未结 6 676
隐瞒了意图╮
隐瞒了意图╮ 2021-01-02 10:03

Sometimes I see code like this:

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

Why comment pa

6条回答
  •  Happy的楠姐
    2021-01-02 10:27

    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.

    I'll use a different example:

    class A
    {
    public:
       virtual void foo(int someProperty);
    };
    
    class B : public A
    {
    public:
       virtual void foo(int /*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);
    }
    

    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.

提交回复
热议问题