I'm trying to write a sort of resource that will make creating command line programs a bit easier. To start off, I created a base class for generic menu types
But I figured that it would make more sense to use that as a base class, then derive a command line-specific one from it (all of my work so far is Command line). So I wrote a class for that...
Obviously I want to use the iterator to work through each member in linkslist, a member vector of the base class that holds pointers to the menus around each menu. The problem appears to be when I try to use the iterator it to access the elements of the member it is accessing. The compiler keeps telling me that the expression must have pointer to class type.
Can anyone shed a bit of light on the situation? My brain is thoroughly knotted from wrestling with this.
I had tried using the *it, but I never enclosed it in brackets.
Virtual Destructors... Gotta check my notes again, oh well...
Oh, and the indentation is just my thing. Allows for the same amount of code onscreen at any time, but much easier to read (at least IMHO). I always hated the standard
function {
statements
}
layout because its disjointed visually
function
{ statement1
statement2 }
allows me to draw a little box around it, using the curly brackets as the points of the rectangle. But I digress...
Thanks again :)
Edit: One last thing. The reason that the iterator was a member was because each menu needed to have an individual list of menus that it could link to. Having the vector externally wouldn't make sense from this perspective.
> Having the vector externally wouldn't make sense from this perspective.
I am not complaining about the vector being a member, I complain about the iterator.
Wait... the iterator is not a member, but a global variable. That's even worst.
Yes, I had to change the structure later on, now the vector and the iterator are external. (I have no idea why I thought the vector had to be menu specific when I was already passing strings to each menu to be compared). :facepalm:
Why exactly is a global iterator a problem?
Just for the record, the code is working beautifully now. I used it to create a wav player in ~3-4 lines yesterday. I can post it if you want to see it.
http://c2.com/cgi/wiki?GlobalVariablesAreBad (I've just read the first section)
Now consider your code for (it = (Linkslist.end()-1); it != (Linkslist.begin()-1); --it)
¿what advantage do you have by making `it' a global?
Please note that you don't care about its previous value, and you left it in an invalid state
By the way, if you want to traverse on the other direction, consider using a reverse iterator for(auto it = Linkslist.rbegin(); it != Linkslist.rend(); ++it)
Okaaay, fair enough, the article does make some good points, but I still dont see any advantage to making the iterator local to each menu object. All that would do would add another minor amount of memory allocation to each object for each menu's interator, when the tasjk at hand only requires one allocator to function anyways?
Ie, why grow memory usage overhead linearly with the number of menu objects, when it could be constant instead? (at least wrt Iterators)
Okaaay, fair enough, the article does make some good points, but I still dont see any advantage to making the iterator local to each menu object.
You're missing the point. The iterator should not be local to each menu object. It should only be present in the function that uses it. There is no global object. There is no extra memory per object.