Why -> and not *name.type

When you are coding with pointers in c++ and you want to access the function/variable from that pointer deferenced, how come you cannot use something like below.

struct typeT{
   int value1;
};
 
typeT* tt = new typeT;
*tt.value1;

it is because the access element of the variable tt is higher in the compiler and thus it tries to equate

tt.value1

first, which is not a good thing because the tt has not been de-referenced and thus it is just a memory address pointer to the actual object. so you need to do

(*tt).value1

because like in maths the () will be equated first and then the access element part “.” and thus to make it easier

tt->value1

will change to

(*tt).value1

within the compiler.

Leave a Reply

Your email address will not be published.