handle reference of struct instance inside class

Hi!

I'm having some troubles to handle different struct instances inside a class.
In summary, the essential code looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
enum class StepperIterator { X1, X2, Y };

struct StepperHandle
{
    StepperPins pins;
    float actual_position_mm;
    uint16_t actual_position_steps;
    float target_position_mm;
    uint16_t target_position_steps;
    float center_position_mm;
    bool is_busy;
    Direction direction;
    ControlState control_state;
};

class Stepper
{
public:

    void foo()
    {
        for (auto stepper : stepper_iterator)
        {
            reset_actual_position(stepper_handle(stepper));
        }
    }

    void reset_actual_position(StepperHandle& stepper)
    {
        stepper.actual_position_mm = 0;
    }

private:
    StepperHandle& stepper_handle(StepperIterator stepper_instance)
    {
        switch(stepper_instance)
        {
            case StepperIterator::X1:
            {
                return x1;
            }

            case StepperIterator::X2:
            {
                return x2;
            }

            case StepperIterator::Y:
            {
                return y;
            }
            default:
            {
                while (true) {}
            }
        }
    }

    StepperHandle x {};
    StepperHandle y {};
    StepperHandle z {};
};



Now the problem is that my StepperHandle instances x,y,z are not handled as this code would describe it. The different struct members are getting reseted when calling the instance in other functions, but always forwarding the instance by stepper_handle(). The advantage of this function is that my Code gets so much shorter instead of calling x,y,z seperately in every function.

I would appreciate if you can see where my instance handling leaks..



Thanks!

Last edited on
what is x1 (not X1, which is your enum item)
Now the problem is that my StepperHandle instances x,y,z are not handled as this code would describe it.
What does that mean?

I would appreciate if you can see where my instance handling leaks..
? It does something wrong? What is it supposed to do and what does it do instead?
Topic archived. No new replies allowed.