C++primer Learning--various operators

Source: Internet
Author: User

prefix increment and suffix increment
Class Newint   {public   :       newint (): Rootint (0) {};       Newint (int iniint): Rootint (Iniint) {};   newint& operator++ ()   {       cout<< "prefix" <<endl;      this->rootint+=1;      return *this;   }   Newint operator++ (int)  //suffix, parameter (int) do not doubt that INT does not have a substantive effect, just to distinguish the prefix, the + + operator is the suffix form  {       cout<< " Postfix "<<endl;      Newint prenum = *this; In the postfix increment operation, first save the original object to an object in the prefix-style method used here;      + + (*this);             The original object is then incremented;      return prenum;         Finally, the original saved meta-object is returned.  }   Private:       int rootint;   }; Source: http://blog.csdn.net/tian_jinping/article/details/9706857
when the suffix is incremented, an original object is saved, then the original object is incremented, and the original object is returned. so the prefix increment is more efficient, and the post type is sometimes a waste. Example: ++a = 22; The first ++a, the return is a, and a is a variable, obviously an lvalue, he can be addressed by the program, can change its value
Where's a++? a++ is an expression that references a temporary object, the user cannot address the object, and cannot change its value, so it is an rvalue, not an lvalue Composite operators:a+=b (a=a+b) any one can be equivalent to a = a op bHowever, the complex operation requires only one value at a time , and the average requires a value of two times. one is evaluated as the right-hand expression, and the other is evaluated as the left-hand operation of the assignment operation. member Operators:The arrow acts on the object of the pointer type, and the result is an lvalue. The point operator is divided into two cases, what is the value of the object to which the member belongs, and what value is returnedConditional operators:
cout<< ((it = = Vec.end ())?" No ":"Yes") << Endl;
/* If two expressions are lvalue or can be converted to the same lvalue return lvalue */sizeof operatorThe resulting is a size_t type, because the object value is not actually computed, so the pointer is invalid ... and it doesn't convert the array into pointers, it returns the size of the entire array .sizeof (IA)/sizeof (*ia) can return the array size
1 sizeof (type) 2 sizeof // returns the size of an expression type 3 sale_data data; 4 sizeof // the size of the data type, which is sizeof (sales_data);
sizeof *p The expression is combined right-to-left because the right binding law is the same as the priority of *. So it is equivalent to sizeof (*P);structure of sizeof:sizeof for structs involves byte alignment issues. Why do I need byte alignment ? It helps to speed up the number of computers, otherwise it will take more time to cycle the instruction. To do this, the compiler will handle the struct by default (as well as data variables elsewhere), so that the base data type of 2 (short, etc.) is located at an address divisible by 2, and the base data type (int, etc.) with a width of 4 is at an address divisible by 4, and so on. In this way, the number of bytes in the middle of the two may need to be added, so the sizeof value of the entire struct increases. byte alignment details are related to compiler implementations, but in general, three guidelines are met:(1) The first address of a struct variable can be divisible by the size of its widest base type member. (2) The offset of each member of the struct relative to the first address of the struct is an integer multiple of the member size, and if necessary, the compiler adds padding bytes (internal adding) between the members. (3) The total size of the struct is an integer multiple of the size of the structure's widest base type member, and if necessary, the compiler adds padding bytes (trailing padding) after the last member.  Note: The sizeof value for an empty struct (without data members) is 1. Imagine how a "space-free" variable is addressed, and how the two different "empty struct" variables can be distinguished, so that the "empty struct" variable is stored so that the compiler can only allocate one byte of space for the placeholder. Reference: http://blog.csdn.net/candyliuxj/article/details/6307814
struct node{    int0;     Char ' ' ;     int 0 ;}; int Main () {    node root;     sizeof (root) << Endl;    System ("pause");}

C++primer Learning--various operators

Related Article

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.