Using classes with the Arduino

前端 未结 7 1888
孤独总比滥情好
孤独总比滥情好 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:34

    On Arduino 1.0, this compiles just fine:

    class A
    {
      public:
       int x;
       virtual void f() { x=1; }
    };
    
    class B : public A
    {
      public:
        int y;
        virtual void f() { x=2; }
    };
    
    
    A *a;
    B *b;
    const int TEST_PIN = 10;
    
    void setup()
    {
       a=new A(); 
       b=new B();
       pinMode(TEST_PIN,OUTPUT);
    }
    
    void loop()
    {
       a->f();
       b->f();
       digitalWrite(TEST_PIN,(a->x == b->x) ? HIGH : LOW);
    }
    

提交回复
热议问题