Using classes with the Arduino

前端 未结 7 1880
孤独总比滥情好
孤独总比滥情好 2020-12-25 11:01

I\'m trying to use class objects with the Arduino, but I keep running into problems. All I want to do is declare a class and create an object of that class. What would an e

7条回答
  •  旧巷少年郎
    2020-12-25 11:24

    http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1230935955 states:

    By default, the Arduino IDE and libraries does not use the operator new and operator delete. It does support malloc() and free(). So the solution is to implement new and delete operators for yourself, to use these functions.

    Code:

    #include  // for malloc and free
    void* operator new(size_t size) { return malloc(size); } 
    void operator delete(void* ptr) { free(ptr); }
    

    This let's you create objects, e.g.

    C* c; // declare variable
    c = new C(); // create instance of class C
    c->M(); // call method M
    delete(c); // free memory
    

    Regards, tamberg

提交回复
热议问题