for (int i = 0, size = names.size(); i < size; ++i)
{
std::cout << names[i] << std::endl;
}
return 0;
}
Output:
Foo
Bar
Okay so what I'm trying to understand is since I'm pushing anonymous stack string objects onto the vector, I'm assuming that the strings will also have their destructors called (because local variables). This should also destroy the internal char sequence.
Yet the strings remain intact when printing the vector (so somehow its being copied?). So /prog/ please show me your sepples mastery and explain the magic here.
>>1 Okay so what I'm trying to understand is since I'm pushing anonymous stack string objects onto the vector,
You are pushing copies of them actually. Which use copies of the string constants themselves. In other words: you have a char* constant "Foo" allocated in the program's const data area, a temporary stack-allocated string object and a heap region containing the copy of "Foo", and an element of a vector<string> holding a pointer to another heap region containing the copy of "Foo". The destruction of the temporary doesn't affect either of the others.
You now also see how C++ allows utilizing every last bit of performance of the hardware!
>> "There are so many steps involved in tweaking the last uumph out of your linux system- and it really is a work of art to pull it off- I have used many different kernels and all sorts of optimization combinations-yesterday I finally used -noatime and -notail for my reiserfs file system: The single biggest performance boost I have yet to see-now I can have gnome2.2 running using gnome-terminal to compile the latest j2sdk from source (nice -n 19)while browsing with mozilla while running e17 in a seperate login with two eterms and run Unreal Tournament at full speed (this with an apache webserver running for my dyndns pseudo-domain and a mysql for my answering-machine software for my isdn card-which keeps track of all incomming phonecalls and manages my telephone book app and ntfsd/sshd/dhcp server/squid)..." VROOM!! VROOOM!!!!
Name:
Anonymous2011-02-24 7:48
C++ is an abomination. Do not user it ever!!! But importantly check my dubs
IIRC, passing anonymous stack objects as method/function arguments has different semantics from passing named stack objects.
The former would result in a call to the following method and the anonymous string object would actually be the named argument, as opposed to being copied into it (only one object would be created): void std::vector<std::string>::push_back(string str);
While the latter result in a call to this method: void std::vector<std::string>::push_back(const string &str);
>>27
Nah, I'm OK if what I said was wrong. As I said, it's possible I'm not remembering it correctly. You're welcome to look for confirmation/refutation yourself, though.