so i'm learning c++ and i just learned about dynamically allocated memory for class. there's something that make me it feel weird.
int main()
{
person* pOne = new person("mike", 35);
cout << pOne << " " << pOne->getName() << endl;
person pTwo = { "dave", 30 };
cout << pTwo.getName() << endl;
return 0;
}
i think that when we want to call getName() function in pOne, we should do it like *pOne->getName()
because pOne hold the memory location, not the person object itself. but if i do that i'll got compiler error.
i do it with pTwo
that not dynamically allocated and it work like i tought.
so, can someone explain the logic of not using "*" when trying to call function?
The built-in operator a->b
is defined as (*a).b
, so the dereference is "hidden" inside the ->
operator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With