Advertisement

arrays of classes

Started by May 26, 2000 01:56 AM
0 comments, last by Fuzz 24 years, 8 months ago
I am making a simple gui. The plan is to have a button class and lots of specific button classes that inherit the button class. In my tab class I have a pointer to a button. When I create buttons to go on the tab I do this: tabButtons = new specificButton[20]; The problem is it seems like only the first button is a specific button, and the rest are just buttons. I access the first like tabButtons[0] and the rest by tabButtons[1] etc... is it because tabButtons are normal buttons and not specific buttons? If so, how do I access the rest of the buttons as specific buttons?
The answer lies in how you originally declared tabButtons. If you declared it as ''Button *tabButtons[20]'' then you will only access it as a button. If you declared it as ''SpecificButton *tabButtons[20]'' then it would access it as specific buttons.

Also, if you do:

tabButtons = new SpecificButton[20];

and tabButtons is defined as:

Button *tabButtons;

then you will only ever see the one specific button object.

A better solution to your problem would be:

SpecificButton *tabButtons[20];

for(int i=0; i<20; i++)
tabButtons = new SpecificButton;

This will create an array of 20 specific button objects
accessible through tabButtons.

You would then access this as:
tabButtons[buttonNum]->MyMemberFunction(someVariable);

To free this array you would do:

for(int i=0; i<20; i++)
delete tabButtons;<br><br>Hope this helps.<br> </i>

This topic is closed to new replies.

Advertisement