2) Pointer to variable is basically is a variable address.
3)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
struct troll
{
troll* operator&() //Operator overloading
{
return (troll*)3735928559;
}
};
int main()
{
troll A, B;
//Note that those two have seemingly same address,
//but if you try to use it it will likely lead to crash
std::cout << &A << ' '<< &B;
}
3) Maybe it's because i haven't studied yet what "operator"... means but sincerely i don't understand what really you are doing. Maybe i should study first the chapter about operators overloading.
TRYING TO UNDERSTAND...
a) What you are doing in the struct is creating a function with a return type that is a pointer to the struct where the function is?
b) Then you convert a constant to a pointer? (is that possible?!)
Well, you will get to it sometime, and then you will understand why your function would fail on it.
Things like &, +, -, * are operators. http://en.cppreference.com/w/cpp/language/expressions#Operators Most of them are not defined for user created classes. You can overload them i.e. add some new behavior to it. That is done by using specially named functions (started with word operator). When you do &A, you expect it to return a pointer to the address where A is. However for user defined classes you can make it do whatever you like. I could make that operator to return string "address is secret" instead.
Then you convert a constant to a pointer? (is that possible?!)
Pointers can be converted to integral value and back. That was that way starting from C. I used decimal constant to hide value which will be output to screen. I could wrote 0xDEADBEEF directly.
c) hmmm... maybe i should study hard!
As I said you will get there eventually. Overloading of & is rarely seen and usually done to help programmer, not to hinder him