79115138

Date: 2024-10-22 17:07:15
Score: 0.5
Natty:
Report link

I managed to make it with a trick. Though I'm not sure it will work as expected independently of compiler implementation. Please post if you see any caveat.

First, add a trivial member to struct, at first position, which must always have the same name (busin this case):

//list of sensors with a member for each sensor
typedef struct {
    cOneSensor_ini bus    = cOneSensor_ini("A4",   0); // <<--- HERE fixed member    
    cOneSensor_f A4_temp1 = cOneSensor_f  ("A4t1", 0x1234);
    //... more sensor of different types
}Sensor_list_4_s;

Now you can use this first member with known name as a pointer to the whole struct: sensors_array = &this->bus;:

template <typename T>
class cSensorsList: public T
{
//...
public:
    cSensorsList(int bus_code):bus_code(bus_code)
    {
        nSensors = sizeof(T);
        p_sensors_array = &this->bus; // <<--- HERE get pointer to template members
    }
    void update(void) {/*...*/}
};

But I'm pretty sure (or I wish) there should be a more elegant solution.

Reasons:
  • Whitelisted phrase (-1.5): you can use
  • RegEx Blacklisted phrase (2.5): Please post
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: piedramania