Jump to content
  • Advertisement
Sign in to follow this  
Gink

Can anyone explain this line?

This topic is 4776 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!