Adafruit_SSD1306 as a member of a class

Hello! I'm writing my own library and I need to control an 128x64 I2C display. I've choosed Adafruit_SSD1306.h one. I made a class what will keep functions for my needs.

But I suddenly caught a problem that Arduino Mega 2560 Pro (which I'm using for my project) freezes when I declare an Adafruit_SSD1306 object inside a class (as it's member). It happens when I just declare my class' object (and also a member object), not when initializing those.

//.h
class Display
  {
  private:
    Adafruit_SSD1306 display;
  public:
    Display();
    void clear();
    void update();
    void drawLine(uint8_t, uint8_t, uint8_t, uint8_t, bool);
    void drawRect(uint8_t, uint8_t, uint8_t, uint8_t, bool, bool);
    void drawCircle(uint8_t, uint8_t, uint8_t, bool, bool);
    void drawTriangle(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, bool, bool);
    void drawText(String, uint8_t, uint8_t);
    void drawText(String, uint8_t, uint8_t, uint8_t, bool);
    void drawInverse(uint8_t, uint8_t, uint8_t, uint8_t);
  };

//.cpp
Display::Display()
{
  this->display = Adafruit_SSD1306(128, 64);
  this->display.begin();
  this->clear();
  this->drawText("Display!", 0, 0);
  this->update();
}
void Display::clear() { this->display.clearDisplay(); }
void Display::update() { this->display.display(); }
//...some other functions

Never rely on other global objects being constructed and ready for use in your constructors for other global objects (as you are when you call begin for the SSD1306 in your Display constructor). That's why you see begin methods in so many Arduino classes.

Move the contents of your Display constructor into a begin method and call that in setup.

Thank you, that worked. I have a question. Can I just declare my object like Display display; in global space and then use constructor inside a setup() function like display = Display(); ? If not, what's the difference between initializing members of a class in a constructor and doing that in separate function (of that class of course) ?

The this-> notation is superfluous.

Also, don't construct your Adafruit_SSD1306 member object that way. Move it to an initializer list and, as mentioned, move the other stuff to your begin() method:

Display::Display() : display(128, 64)  {
}

void Display::begin() {
  display.begin();
  clear();
  drawText("Display!", 0, 0);
  update();
}