I know that pointer means a variable that's store the address of the pointed object. (Tell me if anything wrong, and help me to figure it out please.)Here's the thing that I don't understand.
Suppose we using int*
1 2 3
int *a;
*a = 2;
std::cout << *a;
In this case, the output would be
2
And *a means the value of the address in which a is pointing to.
And the problems comes.
1 2 3 4 5 6 7 8 9 10
class abc
{
public:
void sth(){std::cout << 2};
};
int main()
{
abc* aclass;
aclass->sth();
}
In this example, why we need to use aclass->sth(); to access the class member function, but cannot be *aclass->sth();?
well your explanation is correct but example is not.
imagine you have a box called box a, in that box you can only put an address. So what you do in your code is i am creating a box a, then I am trying to put a value in that box. So obviously that is not going to fit.
what you could do is this:
1 2 3
int value_stored_in_box = 2; // this is normal integer
int* a = value_stored_in_box ; // this is creating pointer, initializing it with address to value_stored_in_box
remember there are at least 2 values connected to normal variable
1. Its address in memory
2. value it stores
with pointers there are 3 "values" connected/associated
1. address where pointer is pointing.
2. value of the address where pointer is pointing
3. pointer its own address