我正在编写要在Arduino中使用的C ++类。其中,我有一个叫做JointedLeg的课程。
JointedLeg.h:
#ifndef ROBOTLEG_JointedLeg_H
#define ROBOTLEG_JointedLeg_H
#include "ServoJoint.h"
class JointedLeg{
private:
//Pointers to the 2 ServoJoint objects for the upper and lower joint
ServoJoint * upperJoint;
ServoJoint * lowerJoint;
//Joint angle directionality follows the right hand rule, i.e. counter-clockwise is positive
//Because positive direction moves the leg forward for the right side and backward for the left side,
//this variable will flip the direction sign for the left side.
//1 if right, -1 if left
int isRight;
//2D array, where each of the four rows represents each phase in the gait cycle and each column of each row
//represents the upper and lower joint angles respectively
//gaitCycle[0][]= home
//gaitCycle[1][]= step forward
//gaitCycle[2][]= step down
//gaitCycle[3][]= step back
double * gaitCycle[4][2];
public:
JointedLeg(ServoJoint & joint1, ServoJoint & joint2, int side, double * givenGait[4][2]);
};
#endif //ROBOTLEG_JointedLeg_H
JointedLeg.cpp:
#include "JointedLeg.h"
JointedLeg::JointedLeg(ServoJoint & joint1, ServoJoint & joint2, int side, double * givenGait[4][2]):upperJoint(joint1), lowerJoint(joint2), isRight(side), gaitCycle(givenGait){}
逻辑上,腿由一定数量的关节或链接组成。为了最大限度地减少内存使用,我不想在JointedLeg类中再次复制构造这些ServoJoint对象和2D数组,所以我将所有变量指针。但是,我不知道如何在构造函数中传递参数以及如何初始化我的指针。对于关节1和关节2,我作为引用传递,我希望当我做upperJoint(joint1)时,指针upperJoint将正确指向对象,joint1,但我不确定是否应该添加地址指针&# 39;&安培;&#39 ;.此外,我将有一个我想指向的2D数组。我是否需要将该数组作为指针传递,或者我可以简单地传递数组名称,因为数组的变量是指向第一个索引的指针?