2015-02-25

Source: Internet
Author: User
Tags define null

The main thing today is to get rid of the operator overloads of C + +, and two small details are not to be done tomorrow. today also looked at the difference between Malloc,free and new,delete, the difference between nullptr and NULL, the const object problem. we say one by one, first of all, to say the difference between malloc,free,new,delete. malloc can allocate memory, but does not call the constructor, new allocates memory and automatically calls the constructor, free frees memory but does not call the destructor, and delete frees memory and calls the destructor. In summary, so our C + + object is still suitable for new out. New girlfriend ("Default"),nullptr and Null differences, first of all, NULL, in C + +, NULL is a macro substitution, we can find the definition, #define NULL 0, that is, during compilation will replace NULL to 0, this is the pit. In contrast, nullptr are typed, and the default is pointer type. Do the experiment on your own to understand. The const object can only call the Const method, which means that the const object call to the Const method cannot change the variable unless the variable is decorated with mutable, in other words, Mutable modified variables can be modified whether or not const. Finally, the operator overload bar, actually learned some before, but the study is not as deep as today, first of all, this is divided into unary operator overloading, two-tuple operator overloading, friend function to overload. What is a unary operator overload, operator++,operator--, which is a unary operator overload. What's the matter? Binary operator overloading, operator+,operator-, which is the two-tuple operator overload. Sometimes it's better to give a few key examples than to tell you a whole bunch of theories, but I can still say the theory.
Operators that can be overloaded
+-*/% ^ & | ~
! = < > + = = *=/=%
^= &= |= << >> >>= <<= = = =
<= >= && | | + +--->* '
[] () new delete new[] delete[]


Operators that cannot be overloaded
. ::. *?: sizeof


= () [] cannot be overloaded with a meta function


in fact, I feel that the above is not necessary to memorize, see more use more, naturally know what is commonly used to AH. Really not, the internet is so developed, we check a bad??
1. Unary operators
Object op or Op object


Overloading is a member function, interpreted as:
Object. Operator OP ()
Operands are implicitly passed by the object objects through the this pointer
Overloading is a friend function, which is interpreted as:
Operator OP (Object)
Operands are provided by the parameter object of the parameter table




2. Binary operators
Objectl op objectr
Overloading is a member function, interpreted as:
Objectl. Operator OP (objectr)
The left operand is passed by objectl through the this pointer, and the right operand is passed by the parameter objectr
Overloading is a friend function, which is interpreted as:
Operator op (objectl, OBJECTR)
Left and right operands are passed by parameter




UF meta function overloading
The friend function overloading operator is commonly used in cases where the operator's left and right operand types are different
The overloads of the friend function are suitable for direct access to the tired private variables



the essence of operator overloading is function invocation, as long as we can convert an expression of an operator to an expression of a function, and we know which parameter corresponds to which parameter. And then it's all about it.
say so much today, wow, it's 12 o'clock more, I go to brush my teeth, feel hurriedly sleep, tomorrow morning, air good words continue to run.

2015-02-25

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.