I'm new to coding and just finished the Compound data types -> Data structures tutorial. In the pointers to structures example I stumbled across a line (24) which slightly confused me.
In previous examples the stringstream command always looked like
stringstream (mystr) >> pmovie->year;
I replaced the original line from the example code with the one above and it did work as well.
Could someone tell me if there is a difference between both versions, please?
Given a value to type A and a different type B, (B)value and B(value) are equivalent if B defines a non-explicit constructor that accepts a parameter of type A. (B)value is the type casting syntax inherited from C.
EDIT: It just occurred to me that perhaps what confused you was the precedence. (stringstream) mystr >> pmovie->year; does not mean (stringstream) (mystr >> pmovie->year);. It means ((stringstream) mystr) >> pmovie->year;.
I wasn't aware of type casting at this point. Since I didn't made it to the Type casting tutorial yet, I expected stringstream to behave like a function.
std::stringstream is a class that inherits std::iostream and uses an internal bufffer instead of an I/O device. It's useful to convert between strings an numbers, for example.
1 2 3 4 5 6 7 8
std::stringstream stream("12");
int i;
stream >> i;
assert(i == 12);
std::stringstream stream2;
stream2 << i * 2;
std::string s = stream2.str();
assert(s == "24");