如果我有一个继承自2个不同类的类,并且我想将新类放入数组中。该数组仅保存指向其中一个继承类的指针。该数组是否会溢出,或者指针数组是否只“包含”从数组所在类继承的类的一部分?
示例:
我有一个名为' screen'它有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);
现在我想创建一个名为' modules'的新类,它继承了两个'屏幕'和'按钮'。我的想法是模块是一个带有它的屏幕的按钮。
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);
如果我向模块对象添加新对象(如新按钮),然后将该对象添加到屏幕,屏幕类中的按钮引用数组是否会溢出?
感谢您的帮助。
答案 0 :(得分:0)
如果我明白你在问什么,那就不。数组只保存指针,这些指针具有固定的大小。它应该可以正常工作,虽然这似乎是一种令人费解的做事方式。
答案 1 :(得分:0)
我认为没有理由为什么模块应该从ButtonTft继承。这会给模块增加不必要的数据。如果您需要从模块访问ButtonTft的受保护成员(这可能是错误的编程),那么请将其变为朋友。除非你在其中放入太多元素,否则数组不会溢出。