Page 1 of 1

Some code questions

PostPosted: 18 Nov 2014, 23:02
by capitol
I'm reading some of the code, and thought it would be good to have a place to collect questions that i have. (because my c++ skills are extremly rusty)

* In AbstractMenuState the void _DrawBottomMenu(); function isn't marked virtual, but subclasses override it, is there a reason to not mark it virtual?

Re: Some code questions

PostPosted: 26 Nov 2014, 02:57
by Roots
Would be helpful if you mentioned the file and line number that you found this function in (src/modes/menu/menu.h, line 114).

You would not want to use the virtual method if, for some reason, if you had a container of pointers to the base class AbstractMenuState but you only wanted to invoke the method _DrawBottomMenu() that is implemented in the base class, not the inheriting classes. If it was virtual, and you called _DrawBottomMenu on a pointer to an AbstractMenuState object, then it would invoke the method for the subclass that overrides it, which may not be the desired behavior. I took a quick glance at the code, however, and didn't see any compelling reason for why this method (and some others in AbstractMenuState) are not virtual. It seems like it makes more sense for this method to be virtual. Maybe Bertram can explain, or maybe you just found a bug/potential bug.

Re: Some code questions

PostPosted: 27 Nov 2014, 15:45
by Bertram
Hi there,

After looking at the code, there is indeed no reason not to make it virtual, and I'll have a go at fixing this asap. Thanks for the catch. :)
Not making it virtual might have been desired at first, I suppose to make the abstract menu draw the bottom menu background, and then let the child class draw the rest in its overridden method, or simply a mistake. ;)

Best regards,

Re: Some code questions

PostPosted: 29 Nov 2014, 06:30
by Roots
You can just instruct each child class' implementation of that method to call the base method first, then add its own drawing code if additional information is needed. Or you could have the child class not call the base class at all if it needs to do something completely custom. That gives you maximum flexibility for each child class to do what it needs.

Re: Some code questions

PostPosted: 29 Nov 2014, 12:57
by Bertram
You can just instruct each child class' implementation of that method to call the base method first, then add its own drawing code if additional information is needed. Or you could have the child class not call the base class at all if it needs to do something completely custom. That gives you maximum flexibility for each child class to do what it needs.


Since the common part of drawing the bottom menu is stated in one line, I'll add this line into the specialized methods.