I don't know myClass *ptr=&obj is used for? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

4th Oct 2016, 12:45 AM
Polly Chen
2 Answers
+ 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.
4th Oct 2016, 1:21 AM
Daniel Rollins
Daniel Rollins - avatar
0
pointers
7th Oct 2016, 9:21 PM
Chaitanya sudhakar
Chaitanya sudhakar - avatar