Assignmentoperator Expression To Complete Assignment

The latest version of this topic can be found at Assignment Operators.

Syntax

Assignment operators store a value in the object designated by the left operand. There are two kinds of assignment operations: simple assignment, in which the value of the second operand is stored in the object specified by the first operand, and compound assignment, in which an arithmetic, shift, or bitwise operation is performed prior to storing the result. All assignment operators in the following table except the = operator are compound assignment operators.

Assignment Operators

OperatorMeaning
=Store the value of the second operand in the object specified by the first operand (simple assignment).
*=Multiply the value of the first operand by the value of the second operand; store the result in the object specified by the first operand.
Divide the value of the first operand by the value of the second operand; store the result in the object specified by the first operand.
Take modulus of the first operand specified by the value of the second operand; store the result in the object specified by the first operand.
Add the value of the second operand to the value of the first operand; store the result in the object specified by the first operand.
–=Subtract the value of the second operand from the value of the first operand; store the result in the object specified by the first operand.
<<=Shift the value of the first operand left the number of bits specified by the value of the second operand; store the result in the object specified by the first operand.
>>=Shift the value of the first operand right the number of bits specified by the value of the second operand; store the result in the object specified by the first operand.
&=Obtain the bitwise AND of the first and second operands; store the result in the object specified by the first operand.
Obtain the bitwise exclusive OR of the first and second operands; store the result in the object specified by the first operand.
Obtain the bitwise inclusive OR of the first and second operands; store the result in the object specified by the first operand.

Operator Keywords

Three of the compound assignment operators have text equivalents. They are:

OperatorEquivalent
&=

There are two ways to access these operator keywords in your programs: include the header file , or compile with the /Za (Disable language extensions) compiler option.

The simple assignment operator (=) causes the value of the second operand to be stored in the object specified by the first operand. If both objects are of arithmetic types, the right operand is converted to the type of the left, prior to storing the value.

Objects of const and volatile types can be assigned to l-values of types that are just volatile or that are neither const nor volatile.

Assignment to objects of class type (struct, union, and class types) is performed by a function named operator=. The default behavior of this operator function is to perform a bitwise copy; however, this behavior can be modified using overloaded operators. (See Overloaded Operators for more information.)

An object of any unambiguously derived class from a given base class can be assigned to an object of the base class. The reverse is not true because there is an implicit conversion from derived class to base class but not from base class to derived class. For example:

Assignments to reference types behave as if the assignment were being made to the object to which the reference points.

For class-type objects, assignment is different from initialization. To illustrate how different assignment and initialization can be, consider the code

The preceding code shows an initializer; it calls the constructor for that takes an argument of type . Given the code

the assignment statement

can have one of the following effects:

  • Call the function operator= for , provided operator= is provided with a argument.

  • Call the explicit conversion function , if such a function exists.

  • Call a constructor , provided such a constructor exists, that takes a argument and copies the result.

The compound assignment operators, shown in the table in Assignment Operators, are specified in the form e1= e2, where e1 is a modifiable l-value not of const type and e2 is one of the following:

  • An arithmetic type

  • A pointer, if is + or –

The e1= e2 form behaves as e1= e1e2, but e1 is evaluated only once.

Compound assignment to an enumerated type generates an error message. If the left operand is of a pointer type, the right operand must be of a pointer type or it must be a constant expression that evaluates to 0. If the left operand is of an integral type, the right operand must not be of a pointer type.

The assignment operators return the value of the object specified by the left operand after the assignment. The resultant type is the type of the left operand. The result of an assignment expression is always an l-value. These operators have right-to-left associativity. The left operand must be a modifiable l-value.

In ANSI C, the result of an assignment expression is not an l-value. Therefore, the legal C++ expression is illegal in C.

Expressions with Binary Operators
C++ Operators
C++ Built-in Operators, Precedence and Associativity
C Assignment Operators

expression assignment-operator expression assignment-operator : one of = *= /= %= += –= <<= >>= &= ^= |=
// expre_Assignment_Operators.cpp // compile with: /EHsc // Demonstrate assignment operators #include <iostream> using namespace std; int main() { int a = 3, b = 6, c = 10, d = 0xAAAA, e = 0x5555; a += b; // a is 9 b %= a; // b is 6 c >>= 1; // c is 5 d |= e; // Bitwise--d is 0xFFFF cout << "a = 3, b = 6, c = 10, d = 0xAAAA, e = 0x5555" << endl << "a += b yields " << a << endl << "b %= a yields " << b << endl << "c >>= 1 yields " << c << endl << "d |= e yields " << hex << d << endl; }
// expre_SimpleAssignment.cpp // compile with: /EHsc #include <iostream> using namespace std; class ABase { public: ABase() { cout << "constructing ABase\n"; } }; class ADerived : public ABase { public: ADerived() { cout << "constructing ADerived\n"; } }; int main() { ABase aBase; ADerived aDerived; aBase = aDerived; // OK aDerived = aBase; // C2679 }
UserType1 A; UserType2 B = A;
UserType1 A; UserType2 B; B = A;

Assignment Operator


The assignment operator is the simplest and the most commonly used operator.

Format of the Assignment Operator


Assignment operator represents a record that contains character "=" (equality sign). To the left of this equality sign we specify the name of a variable, to the right of it we give an expression. The assignment operator is ended with ";" (semicolon).

Variable = Expression; // Assignment operator

You can distinguish the assignment operator from other lines in the text of the program by the presence of the equality sign. You can specify as an expression: a constant, a variable, a function call, or an expression as such.

Execution of the Assignment Operator


Calculate the value of the expression to the right from the equality sign and assign the obtained value to the variable specified to the left of the equality sign.

The assignment operator, like any other operator, is executable. This means that the record composing the assignment operator is executed according to the rule. When executing the operator, the value of the right part is calculated and then assigned to the variable to the left of the equality sign. As a result of execution of the assignment operator, the variable in the left part always takes a new value; this value can be other than or the same as the preceding value of the variable. The expression in the right part of the assignment operator is calculated according to the order of operations (see Operations and Expressions).

Examples of Assignment Operators


In an assignment operator, it is allowed to declare the type of a variable to the left of the equality sign:

intIn = 3;
doubleDo = 2.0;
boolBo = true;
colorCo = 0x008000;
stringSt = "sss";
datetimeDa= D'01.01.2004';

The previously declared variables are used in an assignment operator without specifying their types.

In = 7;
Do = 23.5;
Bo = 0;

In an assignment operator, the type of a variable is not allowed to be declared in the right part of equality sign:

In = int In_2; // Variable type may not be declared in the right part
Do = double Do_2; // Variable type may not be declared in the right part

In an assignment operator, the type of a variable is not allowed to be repeatedly declared.

int In; // Declaration of the type of variable In
int In = In_2; // The repeated declaration of the type of the variable (In) is not allowed

Examples of using the user-defined and standard functions in the right part:

In = My_Function();
Do = Gipo(Do1,Do1);
Bo = IsConnected();
St = ObjectName(0);
Da = TimeCurrent();

The example of using expressions in the right part:

In = (My_Function()+In2)/2;

Do = MathAbs(Do1+Gipo(Do2,5)+2.5);

In calculations in the assignment operator, the typecasting rules are applicable (see Typecasting).

Examples of Assignment Operators in a Short Form


In MQL4, a short form of composing the assignment operators is used, as well. It is the form of assignment operators where we use assignment operations other than assignment operation "=" (equality sign) (see Operations and Expressions). The short-form operators undergo the same rules and limitations. The short form of the assignment operators is used in the code for better visualization. A programmer may, at his or her option, use one or another form of the assignment operator. Any short-form assignment operator can be easily re-written as a normal, full-format assignment operator, the result of its execution being absolutely unchanged.

In /= 33;
In = In/33;

St += "_exp7";
St = St + "_exp7";

 

0 thoughts on “Assignmentoperator Expression To Complete Assignment”

    -->

Leave a Comment

Your email address will not be published. Required fields are marked *