继承和指针数组

Inheritance and pointer arrays

如果我有一个继承自 2 个不同 class 的 class,并且我想将新的 class 放入一个数组中。该数组仅包含指向其中一个继承 classes 的指针。该数组会溢出还是指针数组仅“包含”从 class 继承的 class 的一部分?

示例:

我有一个名为 'screens' 的 class 它有 3 个指针数组来保存对其他对象的引用。

        class Screens
{
        protected:

        //Edit these to fit the amount of elements used to save space :-) Then we don't need to use dynamic (8 bit MCU :-()
        #define NUMBER_OF_BUTTONS 10
        #define NUMBER_OF_GRAPHS 10
        #define NUMBER_OF_TXTS 10

        UTFT *scr;
        UTouch *touch;

        ButtonTft *buttons[NUMBER_OF_BUTTONS];
        GraphTft *graphs[NUMBER_OF_GRAPHS];
        TFT_printer *texts[NUMBER_OF_TXTS];

        uint8_t ellementsButtons;
        uint8_t ellementsGraphs;
        uint8_t ellementsTexts;

        uint8_t addButIndex;
        uint8_t addGrafIndex;
        uint8_t addTxtIndex;


        public:
        Screens(UTFT *_screen);
        void addButton(ButtonTft *but);
        void addGraphs(GraphTft *graf);
        void addText(TFT_printer *txt);
        void printScreen(uint8_t textToPrint = ALL_TXT, graphIDs_e graphToDraw = ALL_GRAPS);
        void printButton(uint8_t index,bool presed = false);
        void printButton(buttonIDs_e buttonID,bool presed = false);
        void printGraph(graphIDs_e graphMode = ALL_GRAPS,bool whatToClear = WHOLE_GRAPH);
        void printTxt(uint8_t txtMode = ALL_TXT);
        ButtonTft* getButton(buttonIDs_e buttonID);
        uint8_t getButtonIndex(buttonIDs_e buttonID);
        ButtonTft* getPressedButton(uint8_t x,uint8_t y);
        GraphTft* getGraph(graphIDs_e graphID);
        GraphTft* getPressedGraph(uint8_t x, uint8_t y);
    `};

然后我有另一个 class 叫做“按钮”,它是屏幕上的一个按钮。

#define PRESSED true

    class ButtonTft
    {
        public:
        ButtonTft(UTFT *TFTdisplay, int xTopL,int yTopL, int xButR, int yButR,buttonIDs_e buttonID);
        ~ButtonTft();
        void drawButton(bool pressed = false);
        void setPos(int x, int y);//This is never used... remove?
        void text(char *txt);
        bool isPressed(uint16_t x, uint16_t y);
        buttonIDs_e getButtonID();

        protected:

        buttonIDs_e _buttonID;
        uint16_t xTopLeft;
        uint16_t yTopLeft;
        uint16_t xButRight;
        uint16_t yBotRight;
        char    *butonText;
        uint16_t color;
        UTFT    *scr;
    };

像这样将按钮添加到屏幕:

/*==============================================================================
| Function Name: addButton
|   Description: Adds a button element to the screen.
|         Input: A pointer to the button element
|        Return: -
*=============================================================================*/
void Screens::addButton(ButtonTft *but)
{
    buttons[addButIndex++] = but;//Add the element to the array
    if (addButIndex == NUMBER_OF_BUTTONS+1) addButIndex = 0;//Should not do this. Avoid adding more than 10! But Safety first!
    if(ellementsButtons++ == NUMBER_OF_BUTTONS+1) ellementsButtons = NUMBER_OF_BUTTONS;//Safety first!
}

并通过引用按钮对象调用该函数:

module5.addButton(&backButton);

现在我想创建一个名为 'modules' 的新 class,它继承自 'screens' 和 'button'。我的想法是,该模块是一个带有屏幕的按钮。

class Modules : public Screens , public ButtonTft
{
 public:
    Modules(UTFT *_display, int xTopL,int yTopL, int xButR, int yButR, buttonIDs_e buttonID);
    bool isConected;
 protected:
};

我将构造函数参数传递给继承的 class,如下所示:

 Modules::Modules(UTFT *_display, int xTopL,int yTopL, int xButR, int yButR, buttonIDs_e buttonID) : Screens (_display) , ButtonTft (_display, xTopL, yTopL, xButR, yButR, buttonID)
{
    isConected = 0;
}

我想将所有模块收集到一个屏幕对象中,这意味着,我想将模块放入 'screen' 对象中的按钮指针数组中。 (因为模块继承自按钮。)我还想向 'module'-对象添加其他按钮,因为它也继承自屏幕-class。

Modules module5(&display,10,75,100,100,MODULE_5);
module5.addButton(&backButton);
mainScreen.addButton(&module5);

如果我将新对象(如新按钮)添加到我的模块对象,然后将该对象添加到屏幕,屏幕中的按钮引用数组 class 会溢出吗?

感谢您的帮助。

如果我明白你在问什么,那就不会。数组仅包含固定大小的指针。它应该可以正常工作,尽管它似乎是一种复杂的做事方式。

我看不出 Modules 应该继承 ButtonTft 的原因。这会给模块添加不必要的数据。如果您需要从模块访问 ButtonTft 的受保护成员(这将是糟糕的编程),请将其添加为好友。数组不会溢出,除非你放入太多元素。