Pass object (template?) by reference

Hi guys,

i am writing a own class for my own motorcontroller and would like to use the ServoInput library from here: https://github.com/dmadison/ServoInput

My problem now is that i want to pass the ServoInputPin object to my class as reference or to create it dynamicly in my class. Something like that:

Declaration described in documentation:

ServoInputPin<2> servo;

MyMotor.h

class MyMotor
{
private:
	ServoInputPin<>* MotorInput;
}

But he ServoInputPin declaration is not a "normal" class for me as beginner :see_no_evil: What does the "<uint8_t>" mean after the class name? And is it possible to plass the "servo" object to a class?

Sry still in the beginning of my c++ learning.

Best regards,

Raphael

that's the template portion. basically the class is parametrised by this. You use that to code once and generate multiple classes at run time.

if you want another class using ServoInputPin, you could use a subclass of ServoInputPin.

template <uint8_t aPin>
class MyMotor : public ServoInputPin< aPin > {
public:
    MyMotor() : ServoInputPin<aPin>() {
        // Constructor for MyMotor
    }

    // •••
};

if you don't want inheritance, then you can define another class which would be templated too and you pass that value to the member variable

template <uint8_t aPin>
class MyMotor {
private:
    ServoInputPin<aPin> servo; // ➜ Use the template parameter for the pin

public:
    MyMotor() : servo() {
    }

  // •••

};

as in both case MyMotor is "templated" you would do

MyMotor<2> frontLeftMotor; 
MyMotor<3> frontRightMotor; 

to create instances

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.