asked on
ASKER
ASKER
ASKER
What about my simple solnIn your simple solution, which would often be preferable to a perverse solution,
ASKER
ASKER
C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.
TRUSTED BY
Since pfoo is a pointer it only contains address of afoo.
Say when you created afoo was created at an address of 0x1000, so the memory for foo object is allocated at this address, so when you do afoo. the compiler knows that no need to jump or go to another address to get members of foo which is allocated for afoo.
But pfoo contains address of afoo, so say pfoo is created at 0x2000, and it contains a values of 0x1000, so when you pfoo. compiler cannot find memory allocated for members of afoo starting at 0x2000, instead it has to jump or indirect itself to 0x1000 to find the members of afoo hence for pfoo - > is required. When you debug the program see the value of pfoo it shall be equal to address of afoo..also try to print the size of pfoo versus size of afoo you shall understand what I am trying to explain.