0
I don't know myClass *ptr=&obj is used for?
As the question above, myClass obj; myClass *ptr=&obj; What can this use for?
2 ответов
+ 5
myClass *ptr will create a pointer object of the class myClass. this pointer works the exact same way as other pointers except in class form. by initializing it to the memory address of obj it will point back to the values of that object. you would use it for calling methods of a classes object such as
Square s1 (3,3); //Assume the constructor takes 2 arguments to represent length and width;
rather than saying something like s.area (); which could return the the area of the square from the given parameters of the constructor, we can say something like.
Sqaure s1 (3,3);
Sqaure *sPtr=&s1;
sPtr->area ();
this does the same thing.
0
pointers