🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

arrays of classes

Started by
0 comments, last by Fuzz 24 years, 1 month 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?
Advertisement
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