On Fri, Oct 21, 2011 at 08:20:56PM +1300, Ralph Versteegen wrote: > On 21 October 2011 10:35, James Paige <[email protected]> wrote: > > I was trying to write some code using vectors (specifically a vector of > > SimpleMenuItem) and I can find lots of examples of how to use v_append > > to add elements to a string vector, integer vector, or float vector, but > > I can't find any examples of how to add elements to vector of a more > > complex type. Is it possible to do this with v_append? or do I have to > > do something else? > > > > --- > > James > > It is possible to append to a vector using object literals, which look > like "Type<TypeA>(16, 3.14159265358979323)". Unfortunately these can't > be used for types containing strings, such as SimpleMenuItems, because > FB sold insufficient of its soul to C++!
Interesting! Out of curiosity, what additional part of its metaphorical soul would FreeBasic had to have sold in order to get string support for this syntax? > So normally to append to such a vector, it's easiest to use v_expand. > Here's an example from my in progress zone triggers work (I see that > v_expand is only used like this in a single place in svn currently) > > WITH *v_expand(menu) > .text = " [ADD NEW TRIGGER]" > .tooltip = "Press ENTER to add" > END WITH Ah! I saw v_expand, but I overlooked the fact that it returns a T ptr. That is cool. I can happily work with that. > (You can look at appendTypeB in vectortest.bas for more on appending.) > > However, I did write add an overloaded append_simplemenu_item > convenience function (or rather, convert the previous one to vectors), > it just got mixed up with my zone triggers stuff so I didn't check it > in until now. I just finished up what I was doing, which was to > finished switching the menu code to BasicMenuItem vectors; it's no > wonder that you had trouble. Sorry about that. No worries, thank you :) > Also, regarding this comment you left: > > 'menu may in fact be a vector of any type inheriting from BasicMenuItem. > +' ^ Apparently this isn't true yet because we can't really do > inheritance yet? [James] > > It is true, but unfortunately you have to manually cast the > SimpleMenuItem vector to a BasicMenuItem vector when passing it to > these 'generic' functions. I believe that FB will allow passing it > without a cast once it supports inheritance. Once again, I see that I > haven't yet checked in any examples of this. I just looked at those cast(BasicMenuItem vector, menu) which makes sense to me. Very interesting. A little unwieldy, but I can live with it. > Once FB supports inheritance (which it has for a while, but they've > refused to merge that branch into the trunk), it could well make sense > to switch to vectors/arrays of pointers to things derived from > BasicMenuItem instead, because then objects will store their type > (RTTI), we'll have the IS operator, and so on. Groovy. --- James _______________________________________________ Ohrrpgce mailing list [email protected] http://lists.motherhamster.org/listinfo.cgi/ohrrpgce-motherhamster.org
