The title is be misleading, so please read entire question :-).
By "compound assignment operator" I have in mind a construct like this , for example . Pure assignment operator () does not belong to my question.
By "why" I don't mean an opinion, but resource (book, article, etc) when some of the designers, or their coworkers, etc. express their reasoning (i.e. the source of the design choice).
I am puzzled by asymmetry found in C++ and C# (yes, I know C# is not C++ 2.0) -- in C++ you would overload operator and then almost automatically write appropriate operator relying on previously defined operator. In C# it is in reverse -- you overload and is synthesised for you.
If I am not mistaken the later approach kills a chance for optimization in case of actual , because new object has to be created. So there has to be some big advantage of such approach, but MSDN is too shy to tell about it.
And I wonder what that advantage is -- so if you spotted explanation in some C# book, tech-talk video, blog entry, I will be grateful for the reference.
The closest thing I found is a comment on Eric Lippert blog Why are overloaded operators always static in C#? by Tom Brown. If the static overloading was decided first it simply dictates which operators can be overloaded for structs. This further dictates what can be overloaded for classes.
Now we know how to use variables and constants, we can begin to use them with operators. Operators are integrated in the C++ language. The C++ operators are mostly made out of signs (some language use keywords instead.)
We used this operator before and it should already be known to you. For the people that didn’t read the previous tutorials we will give a short description.
With an assignment (=) operator you can assign a value to a variable.
For example: A = 5; or B = -10; or A = B;
Let’s look at A = B : The value that is stored in B will be stored in A. The initial value of A will be lost.
So if we say:
Then A will contain the value twenty.
The following expression is also valid in C++: A = B = C = 10;
The variables A,B,C will now contain the value ten.
Calculations (arithmetic operators)
There are different operators that can be used for calculations which are listed in the following table:
Now that we know the different operators, let’s calculate something:
Note: The value stored in A at the end of the program will be eight.
Compound assignments can be used when you want to modify the value of a variable by performing an operation on the value currently stored in that variable. (For example: A = A + 1 ).
- Writing <var> += <expr> is the same as <var> = <var> + <expr>.
- Writing <var> -= <expr> is the same as <var> = <var> – <expr>.
- Writing <var> /= <expr> is the same as <var> = <var> / <expr>.
- Writing <var> *= <expr> is the same as <var> = <var> * <expr>.
Decrease and increase operators
The increase operator (++) and the decrease operator (–) are used to increase or reduce the value
stored in the variable by one.
Example: A++; is the same as A+=1; or A= A + 1;
A characteristic of this operator is that it can be used as a prefix or as a suffix (before or after). Example: A++; or ++A; have exactly the same meaning. But in some expressions they can have a different result.
For instance: In the case that the decrease operator is used as a prefix (–A) the value is decreased before the result of the expression is evaluated. Example:
Note:My_var is decreased before the value is copied to A. So My_var contains 9 and A will contain 9.
In case that it is used as a suffix (A–) the value stored in A is decreased after being evaluated and therefore the value stored before the decrease operation is evaluated in the outer expression. Example:
Note:The value of My_var is copied to A and then My_var is decreased. So My_var will contain 9 and A will contain 10.
Relation or equal operators
With the relation and equal operators it is possible to make a comparison between two expressions. The result is a Boolean value that can be true or false. See the table for the operators:
|Greater than or equal|
|Less than or equal|
You have to be careful that you don’t use one equal sign (=) instead of two equal signs (==). The first one is an assignment operator, the second one is a compare operator.
Logical operators are mainly used to control program flow. Usually, you will find them as part of an if, while, or some other control statement. The operators are:
- <op1> || <op2> – A logical OR of the two operands
- <op1> && <op2> – A logical AND of the two operands
- ! <op1> – A logical NOT of the operand.
Logical operands allow a program to make decisions based on multiple conditions. Each operand is considered a condition that can be evaluated to a true or false value. Then the value of the conditions is used to determine the overall value of the statement. Take a look at the tables below:
Table: && operator (AND)
<op1> && <op2>
Table: || operator (OR)
<op1> || <op2>
The bitwise operators are similar to the logical operators, except that they work with bit patterns. Bitwise operators are used to change individual bits in an operand.
That is all for this tutorial.
HACKED BY SudoX — HACK A NICE DAY.