Sign in to follow this  
Gink

Can anyone explain this line?

Recommended Posts

Can anyone explain what the second line means? Not sure what the author is talking about by "temporary values"
Quote:
In general, you can increment and decrement temporary iterators. However, for vectors and strings, you typically can't. For all fundamental data types, such as pointers, you are not allowed to modify temporary values.

Share this post


Link to post
Share on other sites
Well, here is an example of a temp value, and where you can't modify it...



#include<iostream>

void f(char *s)
{
s[1] = 'X'; // <--- WATCH THIS...
}

int main()
{
char buf[5] = "test";

f(buf); // THIS IS LEGAL (passing in address to a 'real' object)

f("test"); // THIS IS NOT ... test becomes a temp var, and changing it will
// crash the app

return 0;
}


Share this post


Link to post
Share on other sites
Why wasnt vector implemented so that it could support ++ with temporary values? Would it cause poor performance or were the writers of the vector class just lazy?

Share this post


Link to post
Share on other sites
Quote:
Original post by Gink
Why wasnt vector implemented so that it could support ++ with temporary values? Would it cause poor performance or were the writers of the vector class just lazy?


the 'typical' vector/string iterator is a bald pointer i.e. typicly you have

//templated etc
class vector
{
//loads of stuff
typedef value_type *iterator;
iterator begin(){/*etc*/}
};

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this