C and C++, unlike Java and C#, do not automatically zero-initialize memory or object members for performance reasons as there's no point overwriting memory twice if you're just going to set your own value anyway; the downside is you have to be very careful to ensure you don't use uninitialized data first.
To fix your problem, you can either set the member in the constructor, or in the initialization list:
Trie() {
this->color = 0;
this->root = nullptr;
}
Or:
Trie() :
color ( 0 ),
root ( nullptr )
{
}
As for your color
value, consider using a tuple instead because there are no guarantees that int
will be a 32-bit integer (assuming you need to store 0-255 RGB values):
struct RgbColor {
uint8_t r;
uint8_t g;
uint8_t b;
RgbColor() :
RgbColor( 0, 0, 0 ) {
}
RgbColor(uint8_t r, uint8_t g, uint8_t b) :
r(r),
g(g),
b(b) {
}
RgbColor(uint32_t rgb) :
r( ( rgb >> 24 ) & 0xFF ),
g( ( rgb >> 16 ) & 0xFF ),
b( ( rgb >> 8 ) & 0xFF ) {
}
}