【发布时间】: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