tags:

views:

88

answers:

4

For example, is

(const int)* someInt;

valid code?

If so, is that statement different than

const int* someInt;

?

A: 

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.

Brian R. Bondy
Those are _variable definitions_, not casts.
sbi
Not if someInt is defined as int* for example.
Brian R. Bondy
A: 

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.

Quonux
A: 

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;
ttchong
Note: (const int) can be other type as long as someInt is a pointer to something (not necessary int).
ttchong
Also, if you original intention is to make a cast, you can correct the syntax to (const int*) someInt;
ttchong
ttchong: "as long as someInt is a pointer to something".. which can be dereferenced (not `void*`) and can be cast to an int (not `struct mystruct*`)
James Curran
+2  A: 

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.

Konrad Rudolph