Why does the pointer ptr
allows us to access its member function through the dot operator .
while pt
doesn't as it requires the indirection operator ->
?
int n = 5;
test* ptr = new test[n];
ptr[1].print();
test* pt[45];
pt[1] = new test(2,3);
pt[1]->print();
Because they're different things.
ptr
is a pointer of type test*
, it points to the 1st element of the array test[n]
, whose elements are of type test
, then ptr[1]
gives the 2nd element with type test
.
pt
is an array, whose elements are of type test*
, then pt[1]
gives the 2nd element with type test*
.
because ptr
is an array and ptr[1]
alreadey derefrenced the pointer. While pt
is array of pointers you need to derefrence twice.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.