Some code questions

Some code questions

Postby capitol » 18 Nov 2014, 23:02

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?
capitol
 
Posts: 4
Joined: 13 Nov 2014, 23:41

Re: Some code questions

Postby Roots » 26 Nov 2014, 02:57

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.
Image
Roots
 
Posts: 96
Joined: 04 Mar 2010, 21:54

Re: Some code questions

Postby Bertram » 27 Nov 2014, 15:45

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,
User avatar
Bertram
VT Moderator
 
Posts: 1652
Joined: 09 Nov 2012, 12:26

Re: Some code questions

Postby Roots » 29 Nov 2014, 06:30

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.
Image
Roots
 
Posts: 96
Joined: 04 Mar 2010, 21:54

Re: Some code questions

Postby Bertram » 29 Nov 2014, 12:57

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.
User avatar
Bertram
VT Moderator
 
Posts: 1652
Joined: 09 Nov 2012, 12:26

Who is online

Users browsing this forum: No registered users and 1 guest