OOP Constructors as Parameters

Try to run this (typed here, totally untested) with the serial monitor opened at 115200 bauds.

const byte NUM_LEGS = 2;

class Joint {
  public:
    int _number;
    Joint(int number) : _number(number) {
      Serial.print("\t\t\tcreating Joint #"); Serial.println(_number);
    }
    ~Joint() {
      Serial.print("\t\t\tdeleting Joint #"); Serial.println(_number);
    }
    void begin() {}
};

class Leg {
  private:
    Joint* _joint1;
    Joint* _joint2;
    Joint* _joint3;

  public:
    Leg(int j1, int j2, int j3) {
      Serial.println("\t\tcreating Joints");
      _joint1 = new Joint(j1);
      _joint2 = new Joint(j2);
      _joint3 = new Joint(j3);
      Serial.println();
    }

    ~Leg() {
      Serial.println("\t\tdelete Joints");
      delete _joint1;
      delete _joint2;
      delete _joint3;
      Serial.println();
    }

    void begin() {
      _joint1->begin();
      _joint2->begin();
      _joint3->begin();
    }

    void nextStep() {
      Serial.println("nextStep()");
      Serial.println(_joint1->_number);
      Serial.println(_joint2->_number);
      Serial.println(_joint3->_number);
      Serial.println();
    }
};

class Body {
  public:
    Leg* legs[NUM_LEGS];

    Body() {
      Serial.println("\tcreating Legs");
      legs[0] = new Leg(0, 1, 2);
      legs[1] = new Leg(3, 4, 5);
    }


    ~Body() {
      Serial.println("\tdelete Legs");
      for (auto && l : legs) delete l;
    }

    void begin() {
      for (auto && l : legs) l->begin();
    }

    void nextStep() {
      for (auto && l : legs) l->nextStep();
    }
};


void setup() {
  Serial.begin(115200);
  Serial.println("creating Body");
  Body body;
  body.begin();
  body.nextStep();
  // here Body gets deleted as it's local to the function so you'll see messages from the destructors
  Serial.println("deleting Body");
}

void loop() {}

if all goes well you should see messages about creating the Body instance which in turn will instantiate the Legs and the Joints

then nextStep() will be called on the body which will cascade to the joints

then the setup() exits and thus the body instance gets deleted which will trigger all the destructors for your embedded instances

give it a try!