C + + Self-study notes _ several Operators _ C + + Primer

Source: Internet
Author: User

1. Arrow operator

C + + provides a synonym for an expression that includes a point reference operator and a reference operator: the arrow operator (-).

The point operator is used to get the members of a class type object:

ITEM.SAME_ISBN (ITEM2);   // item is a class type Object, SAME_ISBN (Sales_item Item) is the method defined by the object

If you now have pointers or iterators pointing to the Sales_item object, you need to dereference the pointer or iterator before using the dot operator:

Sales_item *sp=&item1; (// Note be enclosed in parentheses because the precedence of the dereference operator is lower than the dot operator

The above code is fine, but error-prone, so the C + + defines the arrow operator.

The code above can be written directly like this:

Sales_item *sp=&item1;sp->same_isbn (ITEM2);

Put the road exercise:

Assuming that ITER is a variable of type vector<string>::iterator, which of the following expressions is appropriate? and explain the behavior of these legal expressions.

(a) *iter++;          (b) (*iter) + +; (c) *iter.empty ();

(d) Iter->empty ();           (e) ++*iter; (f) iter++->empty ();

Answer:

(a) is legal, the post-increment operator takes precedence over the dereference operator, but it is important to note that if there is a return value, the object returned is the object pointed to by the iterator before it is incremented instead of the object that was incremented, although the post-increment is performed first.

(c) is illegal, the priority of the point operator is higher than the dereference operator (the point operator has a high priority ...) ).

(d) is legal, which is what is said above, meaning the empty () method of invoking the object pointed to by ITER.

(e) is illegal, the precedence of the pre-increment operator is equal to the dereference, so follow the order from left to right, so if the *++iter is right.

(f) is legal, first executed and (d) the same operation, after the end of ITER plus 1.

2. Conditional operators

The conditional operator is the only ternary operator in C + +. This explains one of its uses: use nested conditional operators to find the maximum value of three variables and assign the maximum value to max.

int Main () {    int a=7, b=9, c=8;     int max=a>b?a>c?a:c:b>c? B:c;    cout<<max<<Endl;     return 0 ;}

3. sizeof operator

The function of the sizeof operator is to return the length of an object or type name, in bytes .

  · The sizeof operation is guaranteed to be 1 for a char type or an expression with a value of type char.

  · A sizeof operation on a reference type returns the amount of memory space required to hold the object of this reference type.

  · Doing a sizeof operation on the pointer returns the amount of memory needed to hold the pointer; Note: If you want to get the size of the object that the pointer points to, you must dereference the pointer.

  · A sizeof operation on an array is equivalent to multiplying the number of elements on the element by the result of a sizeof operation on its elements. Because sizeof returns the entire array in memory storage length, the number of array elements can be obtained by dividing the results of the sizeof arrays by the result of sizeof's element type.

int x[]; int *p=x;cout<<sizeof(x)/sizeof(*x) <<endl;    // cout<<sizeof(p)/sizeof(*p) <<endl;    // 1

About invoking the sizeof operator on a class object .

Sales_item Item; int a=sizeof(item); cout<<a<<endl;      // a=16

Finished.

C + + Self-study notes _ several Operators _ C + + Primer

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.