gpt4 book ai didi

c++ - 继承和指针数组

转载 作者:太空宇宙 更新时间:2023-11-04 13:40:13 25 4
gpt4 key购买 nike

如果我有一个继承自 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);

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

感谢您的帮助。

最佳答案

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

关于c++ - 继承和指针数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27987224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com