Use (* it). m or it-> m)

Source: Internet
Author: User

From Extended STL

The standard library requires that all iterator whose values are of the aggregation type must support the operator (operator-> () for pointer member selection. The following is an example code using this operator:

Struct X
{
Int x;
};

Some_iterator <X> si =...
Some_iterator <X> I2 =...
Some_iterator <X> end =...

If (end! = Si &&
End! = I2)
{
Si-> x = si2-> x;
}

Standard (C ++-03: 24.1.1; 1) requires that the pointer member selection operator be applied to an iterator. The semantics is equivalent to the dereferencing operator applied to the iterator first, then, the node number member selection operator is applied, that is, it-> m and (* it ). m is equivalent.

It is a pity that using this operator is troublesome.

Suppose we have a container type C, its instance stores the instance of smart pointer type P, and P is used to manage the object lifetime. P defines a release () method for releasing objects early. Further, assume that a release () method is also defined for the type T of the P-manager. In the following code snippet, we want to call the T: release () method through an instance of iteration type I of the container:

C cont =...
I it = cont. begin ();
It-> release ();

Unfortunately, this code does not call the T: release () method. It calls the P: release () method and destroys the T instance. When we use the cont container again, we may encounter various strange problems. The following code truly implements our ideas:

C cont =...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.