Using AccelStepper (and other objects) inside a class

Just got started with Arduino though I’ve been puttering around with other languages for decades.

I’ve got a project that runs a stepper motor for positioning an observatory dome. I started with an existing firmware version that was pretty rough and slowly cut down the code (didn’t believe in functions or classes beyond the most basic uses I guess).

Anyway, I’ve separated the rotator into a mostly stand alone class and now I’ve taking that class out of the .ino and created a .h and .cpp but I’m stumped when it comes to encapsulating the AccelStepper routines within the class. Declared globally in the main program it works fine but I can’t seem to figure out how to declare, instantiate, and initialize AccelStepper using the .h and .cpp

Globally I just have

#include <AccelStepper>
AccelStepper accelStepper(AccelStepper::DRIVER, STEPPER_STEP_PIN, STEPPER_DIRECTION_PIN);

then just use accelStepper().

How would I use that inside a class? Or, if there’s a good tutorial somewhere on this general topic I’d love a link!

Thanks for any tips!

I just had a thought while doing something completely unrelated to programming.

Would I be correct in saying that the container class would only have a pointer to an accelstepper instance and that I'd instantiate and initialize that instance in the main program and set the container's pointer from there?

That's basically what I was doing in C# years ago. They don't talk about pointers but that's basically what was happening.

My last thought made me use a different search and bam! I found what looks like a perfect explanation at

Would I be correct in saying that the container class would only have a pointer to an accelstepper instance and that I'd instantiate and initialize that instance in the main program and set the container's pointer from there?

C++ has objects and instances. An instance is really nothing more than a pointer to an object. The new operator creates the object, and returns a pointer to it. You can pass the pointer to any function. Whether that function is a class method, or not, doesn't matter.

AccelStepper *pStepper = 
    new AccelStepper(AccelStepper::DRIVER,
                               STEPPER_STEP_PIN,
                               STEPPER_DIRECTION_PIN);

Then, you can pass pStepper to any function, including a constructor.