These things I have memorized and know the outcomes when typing them in, but the full scope of understanding escapes me with some details and that is why I ask rather than to have it linger. Until I finally read somewhere else the explanation to gain further insight. As I am sure I will get there anyways given time, but you guys have helped me fast track it.
Below, (Sam's, p196).
|
int myNumbers[5];//Static array of 5 integers
| |
You tell the compiler to allocate a fixed amount of memory to hold five integers
and give you a pointer to the first element in that array that is identified by the name
you assign the array variable. In other words, myNumbers is a pointer to the first element
myNumbers[0].
This program demonstrates that an array is a pointer to the first element in it.
|
So here it states that it "is" a pointer and not "like" a pointer. So the first thing that came into my mind is that it must reserve an 8 byte memory location, since it "is" a pointer. But no, there is no evidence of this additional memory. So then I will ask, so where is this pointer address stored? And then you will tell me that the name is itself the pointer and is itself the address to the first element.
So then really it acts like a pointer in the syntax of the code and usage, but in memory and compiled code it behaves like a reference, where there is no additional memory hit for the pointer value...because the array name itself is the pointer to the first element of the array.
And, I might as well pose this statement here, or question if you refute me.
3) Reference "&" is just an alias to what it refers to and during compile time it gets replaced with the actual thing it was referencing/referring to. And "sizeof("&myRef")" will always give the sizeof whatever it was referencing, and so it is proof there is no additional memory hit for the reference usage. This ties into my statement about arrays above, because there is no additional memory hit for the pointer of the array name.
You have helped me understand though, that "&myChar" is like saying give me the address of the pointer (**, pointer to a pointer) and on the receiving end (func or operator) it can be overloaded for char**:
So that is how it can be overloaded on the other end. Sorry, I don't have too much experience with pointers of pointers, but I do see them when you guys refer to them and I have to investigate them further at some point. That was my missing link to fully understanding.
But now I can easily see that you can overload like this, where as before I wondered how this was possible because in reality they both are just addresses. Somehow the compiler can detect and sniff out a pointer to a pointer and make that distinction.
1 2
|
operator <<(char**) //Can be overloaded for &myChar
operator <<(const char*) //Can be overloaded for &myChar[0]
| |