What's the difference between type(myVar) and (type)myVar?

后端 未结 2 1575
鱼传尺愫
鱼传尺愫 2020-12-01 18:34

I\'m going through the full tutorial at cplusplus.com, coding and compiling each example manually. Regularly, I stumble upon something that leaves me perplexed.

I am

2条回答
  •  旧时难觅i
    2020-12-01 18:51

    The page you cite is not what I would consider a authority on C++ in general.

    Anyway,

    (stringstream) mystr >> pmovie->year;

    casts a std::string to a std::stringstream object. This is a C-style cast. Rather dangerous if you don't know what you are doing. This would create a stringstream object and the value is extracted to pmovie->year next.

    stringstream(mystr) >> yours.year;

    Creates an anonymous std::stringstream object and initializes it with mystr and then the value is extracted to pmovie->year. The object vanishes at the end of its lexical scope which in this case would be the ; at the end of the line.

    Not much of a difference (as others have noted so far) among the two w.r.t class objects.

    On the other hand, with identifiers (of functions/macros) this gets tricky: function (myVar) = x; works irrespective of whether function is an actual function or a macro. However, (function) (myVar) = x; only works for real functions.

    Some standard library identifiers are allowed to have both forms (most notably the tolower and friends) and therefore if you want to invoke the function always then you should go for the former.

提交回复
热议问题