-
Funky operators?
Hi, I was wondering about two things:
What does *& mean? I know it could be interpreted as "A pointer to a reference," but when would one use such an operator? For example:
Code:
ClassV1 *Info; // .. initialize it elsewhere
...
ClassV2*& Info2 = (ClassV2*&)Info;
I don't understand exactly what they're doing and why. (Note that ClassV2 inherits from ClassV1)
The second operator I've seen is !!
For instance:
return !!(some expression); // this is inside a function
It seems to me that it just makes sure that the expression returns a bool? Is this used when you have an expression returning an int but you want to force it to return a bool? That's my guess. Anyways, thanks in advance, any input would be appreciated.
-
*& is actually a reference to a pointer. It's useful when you want to change a pointer that you've passed to a function.
!! will do just as you said, but it's not particularly useful in C++ because most values that can be represented as bools will be automatically cast to the correct type.
-
Re: Funky operators?
Quote:
Originally posted by M0onshine
Hi, I was wondering about two things:
What does *& mean? I know it could be interpreted as "A pointer to a reference," but when would one use such an operator? For example:
Code:
ClassV1 *Info; // .. initialize it elsewhere
...
ClassV2*& Info2 = (ClassV2*&)Info;
I don't understand exactly what they're doing and why. (Note that ClassV2 inherits from ClassV1)
The second operator I've seen is !!
For instance:
return !!(some expression); // this is inside a function
It seems to me that it just makes sure that the expression returns a bool? Is this used when you have an expression returning an int but you want to force it to return a bool? That's my guess. Anyways, thanks in advance, any input would be appreciated.
The first one is a reference to a pointer. Say you want to modify a pointer itself (make it point somewhere else, not just modify the data it points at). You need to pass it by reference or by pointer, so you need to use a reference to a pointer, or a pointer to a pointer.
For the second, if the function's return type was bool, it would not need the !!. This is typically used when you want to return an int that represents true or false. In such a case, when x == 0, !!x == 0, and when x != 0, !!x = 1. It's a way of forcing the value to be 0 or 1 (so you couldn't return 2, for example).