【问题标题】:Inheritance and pointer arrays继承和指针数组
【发布时间】:2015-01-16 15:24:29
【问题描述】:

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

例子:

我有一个名为“screens”的类,它有 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);
    `};

然后我有另一个名为“按钮”的类,它是屏幕上的按钮。

#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);

现在我想创建一个名为“模块”的新类,它继承自“屏幕”和“按钮”。我的想法是该模块是一个带有屏幕的按钮。

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

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

 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;
}

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

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

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

感谢您的帮助。

【问题讨论】:

    标签: c++ arrays pointers inheritance


    【解决方案1】:

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

    【讨论】:

    • 我可能会误入歧途,但我的想法是系统中的每个模块总是有一个按钮和一个与之关联的屏幕。我的想法是我可以将这些(一个按钮和一个屏幕)收集到一个自己的类中,称为模块。然后,我可以将每个模块显示为不同屏幕上的按钮,并在其自己的屏幕中显示所有模块数据(因此每个模块都有一个屏幕和一个按钮)。这样做是个坏主意,还是我应该只创建一个引用屏幕和按钮的模块类?感谢您的回答:-)
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-12
      • 2013-06-13
      相关资源
      最近更新 更多