# What does that mean in C ++ 2

## C ++ programming: operators

### -[To edit]

Gives a negative sign to a numeric value or reverses the sign.

inti = -5; // i receives the value -5intn = -i; // n receives the value 5

### + 

Used to explicitly specify the sign. Since numbers without an explicit sign are always positive, this operator can be omitted. Sometimes useful to explicitly point out the sign.

inti = 5; // i receives the value 5intn = + i; // n receives the value 5

### Arithmetic operators 

The usual arithmetic conversions used to match the types and to determine the type of result.

### + (Addition) 

Adds the values ​​of its operands and returns the result.

inti = 3; intn = i + 5;

### - (subtraction) 

Subtracts the values ​​of its operands and returns the result.

inti = 5; intn = i-3;

### * (Multiplication) 

Multiplies the values ​​of its operands and returns the result.

inti = 5; intn = 2 * i;

### / (Division) 

Divides the values ​​of its operands and returns the result. When dividing integers, any remainder is omitted, so it is not rounded.

inti = 10/3; // i receives the value 3

### % (Modulo) 

Divides the values ​​of its operands and returns the remainder. Can only be applied to integer operands. If at least one operand is negative, the sign of the result depends on the implementation.

inti = 10% 3; // i receives the value 1

### ++ (increment) 

Increases the value of its operand by 1.

i = 2; i ++; // i has the value 3

With regard to the priority, a distinction is made between postfix and prefix notation. The postfix notation (i ++) has a higher priority than the prefix notation (++ i).

### - (Decrement) 

Decreases the value of its operand by 1.

i = 2; i -; // i has the value 1

With regard to the priority, a distinction is made between postfix and prefix notation. The postfix notation (i--) has a higher priority than the prefix notation (--i).

### = (Assignment) 

Assigns the value of the right operand to its left operand.

i = 3; // i has the value 3

### Combined assignment operators 

The combined assignment operators combine the assignment operator (=) with another operator:

• +=
• -=
• *=
• /=
• %=
• &=
• <<=
• >>=
• ^=
• |=

The left operand is used both as the left operand for the assignment and for the other operator.

a + = b;

so means

a = a + (b);

The bracket around b is intended to make it clear that the entire right expression is calculated first.

### == (equality) 

Returns the Boolean value true if the two operands are equal, otherwise false.

### ! = (Inequality) 

Returns the Boolean value true if the two operands are not equal, otherwise false.

### <= (less than or equal to) 

Returns the Boolean value true if the left operand is less than or equal to the right operand, otherwise false.

### > = (greater than or equal to) 

Returns the Boolean value true if the left operand is greater than or equal to the right operand, otherwise false.

### <(smaller) 

Returns the Boolean value true if the left operand is smaller than the right one, otherwise false.

### > (larger) 

Returns the Boolean value true if the left operand is greater than the right, otherwise false.

### <=> (three-way comparison) 

Since C ++ 20: Returns an object that

• is less than 0 if the left operand is less than the right
• equals 0 if the left operand is the same as the right one
• is greater than 0 if the left operand is greater than the right

### && (AND link) 

Combines the two operands and returns true if both operands have the value true, otherwise false.

true true ---- true

If the result can already be predicted after the first operand has been evaluated (i.e. if this is false, the result is certainly false), the second operand is no longer evaluated.

### || (Or link) 

Combines the two operands and returns true if at least one of the two operands has the value true, otherwise false.

true false ---- true

If the result can already be predicted after the first operand has been evaluated (i.e., if this is true, the result is certainly true), the second operand is no longer evaluated.

### ! (Negation) 

Inverts the value of its operands. True becomes false and vice versa.

true ---- false

### & (bitwise AND operation) 

Links each bit of both operands.

1100 1010 ---- 1000

### | (bitwise OR link) 

Combines every bit of both operands.

1100 1010 ---- 1110

### ^ (exclusive OR link) 

Links each bit of both operands.

1100 1010 ---- 0110

### ~ (bitwise negation) 

Negates the value of each bit.

10 -- 01

### << (left shift) 

Shifts the bits of the left operand by the number of places specified by the right operand and fills the places on the right with zeros.

int i = 1; // dual: 00000001 int n = i << 1; // dual: 00000010 = 2

### >> (right shift) 

Shifts the bits of the left operand the number of places specified by the right operand to the right. The digits shifted to the right fall out, so to speak.

int i = 5; // dual: 00000101 int n = i >> 1; // dual: 00000010 = 2 int a = -2; // dual: 11111110 int b = a >> 1; // dual: 11111111 = -1

### * (Pointer dereferencing) 

Dereferences a pointer so that the real content (the address) is not accessed, but the memory area to which it refers.

int variable = 5; int * pointer = & variable; *pointer = 3; // changes the value of variable on 3

### . (Access to members of an object) 

Accesses a member of an object.

obj.member = value; obj.function ();

### -> (access to members of an object via a pointer) 

Dereferences a pointer to an object specified by the left operand and accesses the member specified by the right operand.

obj_pointer-> member = value; obj_pointer-> function ();

This is equivalent to the following notation:

(* obj_pointer) .member = value; (* obj_pointer) .function ();

### :: (qualification) 

Qualification of an identifier (variable, function, class) with its superordinate element (namespace, class).

std :: cout << "Hello!" << std :: endl;

### . * (Access to and simultaneous dereferencing of a member) 

Access and simultaneous dereferencing of a pointer member of an object.

object. * pointer_member = 5;

### -> * (access to and simultaneous dereferencing of a member via a pointer) 

Access and simultaneous dereferencing of a pointer member of an object pointer

obj_zeiger -> * pointer_member = value;

### () (explicit casting) 

Converts the value of the expression to the right of the bracket to the type inside the bracket.

floatf = 3.3; intn = (int) f;
• Depending on the context, the compiler uses a const_cast, static_cast or reinterpret_cast (see below) to implement the instruction.
• This type of conversion comes from C and should be avoided if possible in C ++. Serious errors can occur if a static_cast is expected, but the compiler uses a reinterpret_cast out of the context.
• You cannot only cast from / to built-in data types.
• Constructor notation (only for built-in data types that are not multi-part):
n = int (f);
• Numbers like 3.3 are doubles by default. But if you want to have a float, it is sufficient to write 3.3f.

### static_cast 

Counterpart to C-Casting.

floatf = 3.3; intn = static_cast (f);

The target type is in the angle brackets. If you want to convert pointers to objects in a hierarchy, you should rather use dynamic_cast.

### const_cast 

Enables write access to a constant declared variable.

constintc = 42; int * pc = const_cast (& c);

### dynamic_cast 

Correct conversion of a pointer or a reference to an object of a base class to an object of a derived class. Dynamic_cast can only be used if the class has at least one virtual method. Usually the destructor is made virtual.

classBase {/*...*/}; classDerived: publicBase {/*...*/}; Base * bptr1 = newDerived; Base * bptr2 = newBase; Derived * dptr1 = dynamic_cast (bptr1); / / i. o.Derived * dptr2 = dynamic_cast (bptr2); // Error: bptr2 points to an instance of Base
classBase {/*...*/}; classDerived: publicBase {/*...*/}; Deriveddobj; Basebobj; Base & bref1 = dobj; Base & bref2 = bobj; Derived & dref1 = dynamic_cast (bref1); // i. o.Derived & dref2 = dynamic_cast (bref2); // Error: bref2 references an instance of Base
• This type of conversion only works if the object to be converted is really one of the target type.
• For the opposite direction, the conversion happens implicitly, dynamic_cast is not required.
• Danger: If the conversion of a pointer fails, dynamic_cast returns a null pointer (0)!
• Danger: If the conversion of a reference fails, dynamic_cast throws the exception std :: bad_cast!

### reinterpret_cast 

Most dangerous and powerful cast that is rarely really needed.

Among other things, it can be used to "reinterpret" pointers in data types (and vice versa).

inti = 25; float * fp = reinterpret_cast (i);

This example shows fp to a most likely undefined float variable at the memory address 25.

### typeid 

This operator can be used to query information about a variable, a reference, a (dereferenced) pointer or a class during runtime. The operator returns a reference of type type_info & that is in the header file type info is defined.

#include classBase {/*...*/}; classDerived: publicBase {/*...*/}; Deriveddobj; Base & bref = dobj; if (typeid (bref) == typeid (Derived)) std: : cout << "bref references Derived" << std :: endl;

Danger: In order to be able to use this operator, RTTI must be activated explicitly in most compilers (in the GNU compiler for example with the command line option-frtti), since the introspection requires a lot of effort from the compiler.

### & (Address determination) 

Determines the address of the operand.

inti = 0; int * pointer = & i;

### sizeof (storage requirement determination) 

Determines the memory requirements of a type or an expression.

intn = sizeof (int); intm = sizeofn;

Parentheses must be used for a type as an argument, but not for an expression.

### new (object creation) 

Creates an object of the specified type. Returns a pointer to the new object.

Object * p = newObject (parameter);

### new [] (creating an object array) 

Creates an array of objects.

Object * object_array = newObject [12];

### delete (object destruction) 

Destroys the specified object.

Object * p = newObject; // do something with the object deletep;

Requires a pointer to the object as an argument. The object must have been created with the new operator. Exception: The pointer can also be NULL.

### delete [] (destruction of an object array) 

Destroys the objects in the specified array.

The objects must have been created with the operator new [].

### () (Function call) 

Calling a function and possibly specifying parameters.

function (); object.function (1, "asdf");

### , (List) 

The comma has a double meaning in C ++.
On the one hand, it has the (purely syntactic) task of a separator in function calls and initializations:

intx = function (1,2,3); inty [] = {4,5,6};

On the other hand, it denotes the Sequential operator. The expressions separated by commas are left to right rated. For example

for (intn = 0, m = 0; n <5; n ++, m = 2 * n) {// ...}

is used when the loop is reinitialized first and then run.

### * Pointer declaration 

Creates a pointer to a specific data type.

int * int_pointer; void * void_pointer;

### ?: (Condition) 

The conditional operator (incidentally the only ternary operator, i.e. an operator with three operands) is a shortened form of an if-else construct.

intmax = (a> b)? a: b;

Results in the first operand (in this case a> b) true, the entire expression results in the second operand, otherwise the third. So the above line could be written as follows:

intmax; if (a> b) {max = a;} else {max = b;}

The operand that does not represent the result is not evaluated.

### [] (Indexing) 

Access to a specific element of an array.

### : (Inheritance) 

Inheriting variables and functions of a class

classClass: public base class, public other base class {...};

### : (Initialization) 

Initialization of superclasses and member variables within the constructor definition

Class :: Class (inta, intb) // Constructor with two parameters: Superclass (a), // Forwarding of the parameter 'a' to the superclass constructor_b (b) // Initialization of the member variable '_b' with the value of ' b '{...}

Pointers should not, but may, be assigned new in the initialization. This leads to memory leaks.

Class :: Class (inta, intb) // Constructor with two parameters: array1_ (newint [a]), // Creation of memory for a elements of the type intarray2_ (newint [b]) // Creation of memory for b elements from Type int // array2_ does not get any memory, array1_ is not released {...}

It is better as follows:

Class :: Class (inta, intb) // Constructor with two parameters: array1_ (NULL), array2_ (NULL) // array2_ does not get any memory, array1_ is not released {try {array1_ = newint [a]; // create Memory for a elements of the type intarray2_ = newint [b]; // Creation of memory for b elements of the type int} catch (std :: bad_alloc) {delete [] array1_; delete [] array2_; throw; // a destructor call is made not, because the object was not constructed} ...}

### throw (exception throwing) 

Throws the exception specified as an operand.

throw exception;