A reference is
another name for something.
A reference-to-an-int effectively IS that int.
1) Can references be converted to bool? If so, how? |
Can an int be converted to a bool? That's how you convert a reference-to-an-int to a bool.
Can a double be converted to a bool? That's how you convert a reference-to-a-double to a bool.
Can a string be converted to a bool? That's how you convert a reference-to-a-string to a bool.
Can any type be converted to a bool? That's how you convert a reference-to-an-any-type to a bool.
2) How do you initialize a reference to null? |
You can't.
A reference
is another name for something.
How can you initialise an int to null? You can't.
How can you initialise a double to null? You can't.
How can you initialise a string to null? You can't.
When you "create" a reference, you're not creating a new object. You're saying that this object that already exists has another name.
1 2
|
int i; // this creates an int, called i
int& p = i; // this does not create an int. It says that the int that already exists, named i, now has ANOTHER name. It's called i, and it's also called p
|
The compiler may or may not choose to create extra pointers for itself to help it keep track of references, but that's up to the compiler. A reference is NOT some kind of fancy auto-dereferencing pointer. It's another name for some object.