This question might be a little stupid but I'm still fairly new to C++ and it's been a while since I've last done something with it.
I have a class called LEDBitmap
that is supposed to hold the width, height and data of a bitmap with only ones and zeros.
In the header file I have the following struct:
struct MapData
{
uint8_t width;
uint8_t height;
uint8_t[][] data;
};
As well as the following constructor, destructor and member variable:
class LEDBitmap
{
public:
LEDBitmap(uint8_t width, uint8_t, height, uint8_t data[][]);
LEDBitmap(uint8_t width, uint8_t, height);
virtual ~LEDBitmap() { };
[...]
private: //members
MapData _map;
};
I now want to write the constructors and possibly the destructor and so far I have the following for the first constructor:
//initialize an empty bitmap with only zeros in it
LEDBitmap::LEDBitmap(uint8_t width, uint8_t, height) {
_map.width = width;
_map.height = height;
_map.data = new uint8_t[width][height];
}
Would this implementation work? (probably not) And should I bother actually implementing the destructor?
EDIT:
Adjusted my code according to @gsamaras's suggestion.
_map
used to be *_ptr
before.
EDIT: a friend suggested to use calloc()
instead. I thus now have:
LEDBitmap::LEDBitmap(uint8_t width, uint8_t height) {
_map.width = width;
_map.height = height;
_map.data = calloc(width*height*(sizeof(uint8_t));
}
and
class LEDBitmap
{
public:
LEDBitmap(uint8_t width, uint8_t, height, uint8_t data[][]);
LEDBitmap(uint8_t width, uint8_t, height);
virtual ~LEDBitmap() {
free(_map.data);
};
private: //members
MapData _map;
};