重写-如何减少键入
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;
};
现在,困扰我的是,每次重写基类成员函数时,我都必须重新键入整个参数列表。为什么我不能写这样的东西:
virtual void Muhahahaha override;
编译器只知道我想要什么?当然,除了只有一个哈哈哈哈的情况。。。
有这样的办法吗?
有这样的办法吗?
是的,但你永远不能这样做。
我不会
听着,如果我给你看这个,你照做了,我的声誉就会受损。。。
我承诺
他们会因为我向你展示。。。
请继续。。。?
好的,那么:
#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
捆绑到一个类中呢?像这样:
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;
};
事实上,"每次都必须重新键入整个参数列表"只是一种症状。一旦重构了代码,使得参数列表变短,问题就会自行消失。
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 将OpenCV C++重写为EmguCV C#-如何使用指针
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 在已经使用Git的情况下减少编译时间
- 如何减少花费的时间
- Python中的for循环与C++有何不同
- 在 C++ 中用派生类型重写成员函数
- C++减少modbus_connect超时
- 在模板基类中为继承类中的可选重写生成虚拟方法
- 方法重写线程C++中的概念
- 为重写std::exception的库生成swig接口时出错
- 如何强制从重写方法调用重写的方法基方法?
- 用于C++的静态二进制检测或二进制重写工具和框架
- 如何将 if else 语句重写为 switch 语句
- 如何重写全局方法名称以在调用原始方法之前将我的代码推到前面
- 在C++中释放内存期间,迭代器与指针有何不同
- 用 C 重写C++类
- 是否总是可以将使用递归编写的程序重写为不使用递归的程序C++,性能观点是什么?
- 重写虚拟函数和继承
- 重写-如何减少键入