[英]C++ Constructors, Constants, Inheritance and Avoiding Repetition
so I've been learning C++ for a few weeks now but I'm having a bit of trouble: 所以我已经学习C ++几周了,但是我有点麻烦:
Class Tool
{
public:
Tool(const float maxCarried = 1):maxAmountCarried(maxCarried){}
virtual void Use() = 0;
/* ... */
}
Class CuttingTool: public Tool
{
public:
CuttingTool(const float maxCarried):Tool(maxCarried){}
virtual void Use(){ /* ... */ }
/* ... */
}
Class Saw: public CuttingTool
{
public:
Saw(const float maxCarried):CuttingTool(1){}
virtual void Use(){ /* ... */ }
/* ... */
}
Class Scissors: public Fruit
{
public:
Scissors(const float maxCarried):CuttingTool(2){}
virtual void Use(){ /* ... */ }
/* ... */
}
A few things to note: 注意事项:
The problem lies within the fact that I have to keep writing: 问题在于我必须继续写:
ClassName(const float maxCarried):BaseClass(maxCarried){}
It's really tedious, moreover, I worry that if I were to add a new const value I would have to repeat the process all over again (problem when you have 50 classes inheriting from Food :S). 而且,这真的很乏味,我担心如果我要添加一个新的const值,我将不得不再次重复该过程(当您有50个继承自Food:S的类时会出现问题)。
I feel as though I've designed this poorly. 我觉得我的设计似乎很差。 Is there a way to avoid repeating the same line of code over and over again or do I just have to suck it up and deal with it?
有没有一种方法可以避免一遍又一遍地重复同一行代码,还是我只需要吸收它并加以处理?
Thanks in advance. 提前致谢。
If your only concern is the repeating initialization list you could use a macro like this: 如果您唯一关心的是重复的初始化列表,则可以使用如下宏:
#define DEFAULT_CONSTRUCTOR(Child, Parent) Child(float max) : Parent(max) {}
and use it like so: 并像这样使用它:
class Saw : public CuttingTool
{
public:
DEFAULT_CONSTRUCTOR(Saw, CuttingTool) {}
};
You can extend this idea and do something like that: 您可以扩展这个想法并执行类似的操作:
#define BEGIN_CLASS(Child, Parent) class Child : public Parent { \
public: \
Child(float max) : Parent(max) {}
#define END_CLASS };
and declare your classes: 并声明您的课程:
BEGIN_CLASS(Scissors, Tool)
void cut_through_paper() {} // specific method for scissors
END_CLASS
Note that there is no point of using const float
as a parameter since you can't change arguments passed by value anyway. 请注意,将
const float
用作参数毫无意义,因为无论如何您都无法更改通过值传递的参数。 You might however want to use const float&
to pass an argument by reference, and that will make sense if size of float is bigger than the size of a pointer in your specific platform. 但是,您可能想使用
const float&
通过引用传递参数,如果float的大小大于特定平台中指针的大小,这将是有意义的。
If you never change you max value, you can make it static and share it between all tool instances: 如果您从未更改最大值,则可以将其设为静态并在所有工具实例之间共享:
class Tool
{
protected:
static const float _max;
public:
Tool() {}
};
const float Tool::_max = 0;
If you'd like to be able to change max value only once (say at the begining of your program, you can add a static function: 如果您只想更改一次最大值(例如在程序开始时,则可以添加一个静态函数:
static void globalMax(float max) { Tool::_max = max; }
and use it where appropriate: 并在适当的地方使用它:
int main() {
Tool::globalMax(5);
...
...
}
Note that you should remove the const
from the _max
declaration. 请注意,您应该从
_max
声明中删除const
。
Finally, if performance is an issue, you probably need to rethink your design and maybe go with something else (templates maybe?) 最后,如果性能是一个问题,那么您可能需要重新考虑您的设计,并可能选择其他东西(也许是模板?)
Hope that helps! 希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.