覆盖 - 如何用更少的打字来做到这一点?

Overriding - how to do it with less typing?

假设我有

class A
{
public:
   virtual void Muhahahaha(int a, float f, Object C, Smth else, Etc etc);
};

class B: public A
{
public:
   virtual void Muhahahaha(int a, float f, Object C, Smth else, Etc etc) override;
};

现在,令我困扰的是,每次重写 base-class 成员函数时,我都必须重新键入整个参数列表。为什么我不能写这样的东西:

virtual void Muhahahaha override;

编译器只知道我想要什么?嗯,当然不包括里面只有一个姆哈哈哈哈的情况……

有这样的方法吗?

Is there such a way?

是的,但你绝不能这样做。

I won't

你看,如果我给你看,你照做了,我的名声会受损...

I promise

他们会因为我向您展示...

而对我投反对票

Go on, please...?

那好吧:

#define OBFUSCATE Muhahahaha(int a, float f, double C, char etc)

class A
{
public:
    virtual void OBFUSCATE;
};

class B: public A
{
public:
    virtual void OBFUSCATE override;
};

void A::OBFUSCATE
{
    // try to debug this
}

void B::OBFUSCATE
{
    // try to debug this too!
}

问题是您在单个函数中使用了太多参数。这对每个功能都不利,无论是否为虚拟。

作为通常可以缓解此问题的解决方案的示例,为什么不将 int a, float f, Object C, Smth else, Etc etc 捆绑到 class 中呢?像这样:

struct Parameters
{
    int a;
    float f;
    Object C;
    Smth Else;
    Etc etc;
};

这样你就可以少打字了:

class A
{
public:
   virtual void Muhahahaha(Parameters const& arguments);
};

class B : public A
{
public:
   void Muhahahaha(Parameters const& arguments) override;
};

"have to retype the whole argument list every time" 的事实只是一个症状。一旦重构代码使参数列表变短,问题就会自行消失。