Operator (operator) is used to manipulate data. Operators to compute, check the equation, perform assignments, manipulate variables, and do other, more bizarre work. There are many operators in C + + that do not want to list all, but list the most
The 11.5.1 of the problem
In object-oriented program design, defining a class yourself is equivalent to creating a new type. Class can be passed as a parameter or as a return type, as is the case with a variable.
In the seventh chapter, we
Why is operator= worth noticing?Syntactically speaking, the following program can be compiled and passed, and I use it in another note example.classa1{ Public: int operator=(intA//The argument is int, the syntax is passed but the logic does not
Let's take a look at the following code:#include using namespace Std;class point{public:point (float x=0.0,float y=0.0): _x (x), _y (y) {}point & operator= (const point& RHS), void Printdata () {coutThe program output results are as follows:Parsing:
The decision between Member and Non-memberThe binary Operators = (Assignment), [] (array subscription), -> (member access), as well as the N-ary () (fu Nction call) operator, must always be implemented as member functions, because the syntax of the
The assignment operator overload function of a custom class acts like a built-in assignment operator, note, however, that it is the same as a copy constructor and a destructor to note the problem of deep copy of a shallow copy, and without a deep
Precedence
Operator
Description
Example
associativity
1
()[]-.::++--
Grouping operatorArray AccessMember access from a pointerMember access from a objectScoping operatorPost-incrementPost-decrement
(A
Overload operator parsing (original)
Overloaded operators are a good young man, but to spit it out, we often write many repetitive code for overloaded operators. It's boring, but it's also necessary. The more you overload, the greater the
recently in writing a class, very depressing is the need to overload = operator, on the internet for a long time did not have a good tutorial, only to explore their own, the original code is written like this://define Class class cstring {
The purpose of overloaded operators:
The operations object for predefined operators in C + + is limited to basic built-in data types
Most of the time we need to do similar operations on our similar types, and this time we need to redefine the
Assignment operators can also be inherited.
The operator of a base class can access the use of a quilt class
Examples are as follows
#include
using namespace std;
Can operators be inherited??
Class A {public
:
a& operator= (const a& obj) {
priority_queue is relatively simple to use for basic types. His template declaration has three parameters, type is the data type, Container is the container that holds the data, functional is the element comparison method. Container must be an array-
What is an overload of an operator?
Operators are combined with classes to produce new meanings.
Why to introduce operator overloading.
Function: To implement the polymorphism of a class (polymorphism means that a function name has many
Priority Level
operator
Description
Example
combination of
1
()[]->.::++--
Bracket operator for adjusting precedenceArray Subscript access operatorTo access a member's operator by pointing to an object's pointerTo
Priority Level
operator
Description
Example
combination of
1
()[]->.::++--
Bracket operator for adjusting precedenceArray Subscript access operatorTo access a member's operator by pointing to an object's pointerTo
operator overloading, objects, and pointers to objects
Directly on the code
Copy Code code as follows:
#include
using namespace Std;
Class Test
{
Public
int A;
Test (): A (0) {}
Test &operator* () {
cout cout return *this;
}
The as operator is similar to a type conversion, but when the conversion fails, the AS operator produces null instead of throwing an exception. In form, the expression of this type:
Expression_r as type
The as operator only performs reference
Off-topic, put a white noise site Rainy Mood, when you can listen to the code, as if there is noAnywayIn the post of C + + replication control, the function that mentions the operator is actually defined . This can actually be generalized to other
1. | | && is right-associative2. + +--is calculated before the end of the entire expression operation or the start. is not just an operator.int i=1,j;j=i+++i+++i++; i=4,j=3;int i=1,m;M=++i+++i+++i; i=4,m=12;3.a+++b;The result is (a++) +b or A + (++b)
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.