June 2025
Intermediate to advanced
1093 pages
33h 24m
English
Whenever I have shown you an example with raw pointers, I have tried to make sure to discourage you from using them. That was intentionally phrased a bit too strictly. You can (or even should) use such a pointer when the pointer does not own the object, meaning it is not responsible for its removal via delete:
int getSize(const Rect *r) { return r->size(); } // raw pointer as argumentint main() { Rect srect{8,12}; cout << getSize( &srect ); // get address of stack object unique_ptr<Rect> urect{new Rect{10,20}}; cout << getSize( urect.get() ); // get raw pointer from smart pointer}
It goes without saying that you could just as well have used a reference as a parameter here:
int getSize(const Rect &r) { return r.size(); } ...
Read now
Unlock full access