Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Obviously it is a pointer, why use the dot operator

The fifth edition of c++ primer describes weak_ptr like this:

A weak_ptr is a smart pointer that does not control the lifetime of the object to which it points.Instead,a weak_ptr points to an object that is managed by a shared_ptr.

But why dot operators are used instead of -> when operations such as reset, use_count, and lock are called?
For example:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

weak_ptr<int> w(sp);
w.use_count();
w.lock();

>Solution :

std::weak_ptr is a class type, and like any class the . operator is used to access its members via a non-pointer instance of that class.

However, unlike other smart pointer classes, std::weak_ptr does not have an operator-> (or operator*) defined to access the object it refers to. You have to first lock() it to gain access to its associated std::share_ptr, which has those operators defined to access the referred object.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading