在构造函数上传递参数或在每个方法上传递它们
Pass parameter on constructor or pass them on each method
我正在学习 C++,我有这个头文件:
#ifndef _ROBOT_MOVE_H__
#define _ROBOT_MOVE_H__ 1
#include "ros/ros.h"
class RobotMove
{
private:
double current_linear;
double current_angular;
public:
void TurnLeft(const ros::Publisher& publisher, ros::Rate& loop_rate);
void TurnRight(const ros::Publisher& publisher, ros::Rate& loop_rate);
void TurnAround(const ros::Publisher& publisher, ros::Rate& loop_rate);
void Forward(const ros::Publisher& publisher, ros::Rate& loop_rate);
void Backward(const ros::Publisher& publisher, ros::Rate& loop_rate);
void Stop();
RobotMove();
~RobotMove();
}
#endif
我不知道是在构造函数上传递一次 const ros::Publisher& publisher, ros::Rate& loop_rate
还是每次调用其他方法时都传递它们更好。
是在构造函数中传递这两个参数并在 class 中存储一个指针,还是在每个方法中传递它们更好?
顺便说一下,这两个参数将始终是相同的对象。
我想说,从编程的角度来看,您越少需要一遍又一遍地复制和粘贴相同的代码越好。这就是编程的意义所在,让计算机为你重复一些事情。我会将这些参数传递给构造函数,然后将它们存储为属性。我相信你会像下面这样实现它:
class RobotMove
{
private:
double current_linear;
double current_angular;
const ros::Publisher * publisher;
ros::Rate * loop_rate;
public:
RobotMove(const ros::Publisher& publisher, ros::Rate& loop_rate)
{
this->publisher = &publisher;
this->loop_rate = &loop_rate;
}
};
By the way, the two parameter will be always the same objects.
对我来说,这表明它们应该是 class 的成员变量。使它们成为 class 的成员变量遵循 不要重复自己(DRY)
原则。因此,将它们传递给构造函数并让对象将它们存储为成员变量是有意义的。
这是一个很好的基础问题。
要回答这个问题你可以问问自己are these parameters specific to each action(method) or are they objects used by an instance of the class in general ?
在你的例子中给出了你的 class 和你的方法的名称,事实上有这么多方法使用它们并且你另外指定它们不会改变,我建议将它们传递给构造函数而不是每次通话。
虽然这并不总是正确的答案,但您必须根据您认为这些参数可能改变的频率以及您想要赋予 class 答案的语义来判断和平衡它与这些参数的关系。
我知道这听起来像是一堆废话,但设计选择通常取决于上下文。
我正在学习 C++,我有这个头文件:
#ifndef _ROBOT_MOVE_H__
#define _ROBOT_MOVE_H__ 1
#include "ros/ros.h"
class RobotMove
{
private:
double current_linear;
double current_angular;
public:
void TurnLeft(const ros::Publisher& publisher, ros::Rate& loop_rate);
void TurnRight(const ros::Publisher& publisher, ros::Rate& loop_rate);
void TurnAround(const ros::Publisher& publisher, ros::Rate& loop_rate);
void Forward(const ros::Publisher& publisher, ros::Rate& loop_rate);
void Backward(const ros::Publisher& publisher, ros::Rate& loop_rate);
void Stop();
RobotMove();
~RobotMove();
}
#endif
我不知道是在构造函数上传递一次 const ros::Publisher& publisher, ros::Rate& loop_rate
还是每次调用其他方法时都传递它们更好。
是在构造函数中传递这两个参数并在 class 中存储一个指针,还是在每个方法中传递它们更好?
顺便说一下,这两个参数将始终是相同的对象。
我想说,从编程的角度来看,您越少需要一遍又一遍地复制和粘贴相同的代码越好。这就是编程的意义所在,让计算机为你重复一些事情。我会将这些参数传递给构造函数,然后将它们存储为属性。我相信你会像下面这样实现它:
class RobotMove
{
private:
double current_linear;
double current_angular;
const ros::Publisher * publisher;
ros::Rate * loop_rate;
public:
RobotMove(const ros::Publisher& publisher, ros::Rate& loop_rate)
{
this->publisher = &publisher;
this->loop_rate = &loop_rate;
}
};
By the way, the two parameter will be always the same objects.
对我来说,这表明它们应该是 class 的成员变量。使它们成为 class 的成员变量遵循 不要重复自己(DRY) 原则。因此,将它们传递给构造函数并让对象将它们存储为成员变量是有意义的。
这是一个很好的基础问题。
要回答这个问题你可以问问自己are these parameters specific to each action(method) or are they objects used by an instance of the class in general ?
在你的例子中给出了你的 class 和你的方法的名称,事实上有这么多方法使用它们并且你另外指定它们不会改变,我建议将它们传递给构造函数而不是每次通话。
虽然这并不总是正确的答案,但您必须根据您认为这些参数可能改变的频率以及您想要赋予 class 答案的语义来判断和平衡它与这些参数的关系。
我知道这听起来像是一堆废话,但设计选择通常取决于上下文。