访问父级及其父级

时间:2018-05-13 21:41:18

标签: c++ arduino

我很难让孩子班级能够引用其父母。 这是一个更简单的新代码示例:

https://www.googleapis.com/auth/userinfo.profile

编译器输出:

class AlphaClass {
public:
    AlphaClass(){}
    ~AlphaClass(){}
    void init() {
        alpha++;
    }
    int alpha = 0;
};

class BetaClass : public AlphaClass {
public:
    void init() {
        beta++;
        alpha++;
    }
    int beta = 0;
};

1 个答案:

答案 0 :(得分:-1)

问题源于抽象构造函数和析构函数。当我删除所有"虚拟"键,它工作正常:

class FooBar {
    public:
        char foobar[7] = { 'f','o','o','b','a','r' };   //  Should be publically visible
        FooBar() {
            Serial.println("\nConstructing FooBar");
        }
        void begin() {
            Serial.println("\nFrom FooBar:");
            Serial.print("foobar=");
            Serial.println(foobar);
        }
    };

    class Foo : public FooBar {
    public:
        const char foo[4] = { 'f','o','o' };

        Foo() {
            Serial.println("\nConstructing Foo");
        }

        void begin() {
            Serial.println("\nFrom Foo:");

            Serial.print("foo=");
            Serial.println(foo);

            Serial.print("FooBar=");
            Serial.println(FooBar::foobar);
        }

    };

    class Bar : public Foo {
    public:
        const char bar[4] = { 'b', 'a', 'r' };

        Bar() {
            Serial.println("\nConstructing Bar");
        }

        void begin() {
            Serial.println("\nFrom Bar:");

            Serial.print("bar=");
            Serial.println(bar);

            Serial.print("foo=");
            Serial.println(foo);

            Serial.print("FooBar=");
            Serial.println(foobar);
        }
    };

    FooBar *foobar;
    Foo *foo;
    Bar *bar;

    void setup() {
        Serial.begin(115200);
        Serial.println("Beginning...");

        foobar = new FooBar();
        foo = new Foo();
        bar = new Bar();

        foobar->begin();
        foo->begin();
        bar->begin();
    }

    void loop() {

    }