What about C ++ pointers?

I am having 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 next step: ABC *q;
What does it do? My book says it is a class pointer (it is very vague with pathetic grammar). But what does this mean? Pointer pointing to an ABC class address?

If so, the next step bothers me. q = &obj.data;
Therefore, we point this pointer to the location of the data, which is a variable. How does this ABC *q;fit in then?

And the last step. What does it do q->getdata();? My book says that it is a "pointer to a member operator", but does not provide an explanation.

Glad to get any help!

+4
source share
4
ABC * q

, ABC. :

q = new ABC();

ABC q.

ABC abc;
q = &abc;

ABC ( , ) q.

, -> -. - :

a -> b

(*a).b

, a ( , ++ ), ( ) , a->b = 5; a->DoSth(); (*a).b = 5; (*a).DoSth();.

+3

, :

ABC *q;
q = &obj;
q->getdata();

int:

ABC *q;
int *qq;
qq = &obj.data;
q = &obj;
q->getdata();
+5

q = &obj.data, "Lame-up-duck", :

:

ABC *q;

, 32 64 . , ABC

, obj.

"" , " " &, :

q = &obj;

q obj. obj

q->

q->getdata() getdata obj.

+2
ABC obj;
ABC *q;
q = &obj.data;

.

q = &obj;

which assigns an address obj(class instance ABC) to a pointer to an ABC called q.

Once this is done, if you want to call getdata on q, you use

q -> getdata();

... because q is a pointer. Compare this to what you would do with obj(which is a stack variable)

obj.getdata();

The same function called in different ways.

+2
source

All Articles