I'm having some trouble understanding the use of pointers in this program:
#include<iostream.h>
class ABC
{
public:
int data;
void getdata()
{
cout<<"Enter data: ";
cin>>data;
cout<<"The number entered is: "<<data;
}
};
void main()
{
ABC obj;
int *p;
obj.data = 4;
p = &obj.data;
cout<<*p;
ABC *q;
q = &obj.data;
q->getdata();
}
I get everything until the following step : ABC *q;
What does that do? My book says it's a class-type pointer (it's very vague with pathetic grammar). But what does that mean? A pointer pointing to the address of the class ABC?
If it is, then the next step confuses me. q = &obj.data;
So we're pointing this pointer to the location of data, which is a variable. How does that ABC *q; fit in, then?
And the last step. What does q->getdata(); do? My book says it's a 'pointer to member function operator', but gives no explanation.
Glad to recieve any help!
q = &obj.dataequalsq = &obj?q=&obj. Secondlyq->getdata()will invoke the method for that instance of ABC