sizeof() on custom library constructor returns wrong result I made a custom library (let's call it DigitalOutput.h) to learn a bit about librairies and to simplify my work. Here is the basic constructor in the .h file:  

DigitalOutput(uint8_t pin);


Here is the code in the .cpp file :

DigitalOutput::DigitalOutput(uint8_t pin)
{
    _pin    = pin;
    _status = LOW;
    pinMode(pin, OUTPUT);
}


So very simple stuff. Now, in my program, I call the function the usual way:

DigitalOutput LED(7); // pin #7 will be configured 
                      // as an output pin to control the LED


Now the issue comes when I create an array and then use the sizeof() function.
Here is the code from my program (using an Arduino Mega):

DigitalOutput   LED_array[] = {22,23,24,25,26,27,28,29,
                               30,31,32,33,34,35,36,37};


So, the array contains 16 elements. But when I do for instance...

#define         LED_NUMBER    sizeof(LED_array)
//...
void setup(){
    //...
    Serial.println(sizeof(LED_array));
    Serial.println(LE_NUMBER);
}


...I get 160 as a result in both cases instead of 16...

So I end up doing a weird #define LED_NUMBER sizeof(LED_array) / 10 in order to get the proper result which is then used throughout the program. Any idea of why this isn't working right?
It is working perfectly correctly.

sizeof doesn't return the number of elements in an array, it returns the number of bytes the array takes up. Since each entry in your array is an instance of your DigitalOutput class, the size is the number of elements multiplied by the size of the class - in this case 10 bytes.

To get the number of elements you must divide the number of bytes by the size of one element:

int numEles = sizeof(LED_array) / sizeof(DigitalOutput);


or

int numEles = sizeof(LED_array) / sizeof(LED_array[0]);