开发者

Can parentheses take arbitrary identifiers as arguments? C++

开发者 https://www.devze.com 2023-01-07 07:42 出处:网络
For ex开发者_Python百科ample, is (const int)* someInt; valid code? If so, is that statement different than

For ex开发者_Python百科ample, is

(const int)* someInt;

valid code?

If so, is that statement different than

const int* someInt;

?


You can put arbitrarily many parentheses around expressions without changing the meaning. But you cannot do the same with types. In particular, as the others have pointed out, the parenthese in your code change the meaning from a declaration to a cast.


You can do a c style cast with any type inside, but the expression that you are trying to cast may not be able to be casted that way.

You can't have any arbitrary type on the right hand side of a cast. You need a user defined conversion operator to perform the conversion.


This seems for me valid, because you can have everytime a pointer to an constant value.

I don't think that between the two exists an difference.


if someInt is defined as

int *someInt;

then

(const int)* someInt;

is valid. Else you will encounter error.

You deference a pointer to int and cast the resulting value to const int. And yes, this statement without an assignment, is wasted.

int rtn = (const int)* someInt;
0

精彩评论

暂无评论...
验证码 换一张
取 消