C++ operators are essential for pro­gram­ming with C++. Whether you are just beginning to learn C++ or have been working with the pro­gram­ming language for a while, operators can make pro­gram­ming simpler and more efficient. The function of some operators can be deduced from their name, while others may take some time to memorise.

What are C++ operators?

An operator is a sign for an operation, which is applied to at least one operand, but in most cases to several. This results in a new value in most cases. A popular example of these are the arith­met­ic operators you learn in school, for example, ‘+’ for addition and ‘-‘ for sub­trac­tion.

C++ operators are not only dis­tin­guish­able according to their func­tion­al purpose. The operator’s arity is also an important criterion:

Arity Number of operands Example
Unary operators One operand Logical negation: !var_name
Binary operators Two operand Addition: value1 + value2
Ternary operators Three operands If-else-condition: condition? condition_true : condition_false

What is operator pre­ced­ence?

As with arith­met­ic operators in school or Python operators, there is an operator pre­ced­ence for C++ operators. This specifies the order in which the operators should be evaluated. The dot before dash rule applies for arith­met­ic operators, however, there are other rules for other C++ operators.

if (var1 && var2 || var3) {
    do_something();
}
cpp

The example above shows the logical ex­pres­sion being evaluated after the if operator. The && operator (logical And) has priority over the || operator (logical Or). So, if the eval­u­ation of ‘var1 && var2’ or the eval­u­ation of ‘var3’ returns ‘true’, the ‘do_something()’ function call is executed.

You can also use brackets to be on the safe side.

How to overload C++ operators

You can overload most C++ operators. This means that you can assign a new meaning to an existing operator in a context. To perform an operator overload in C++, you need the keyword ‘operator’. When over­load­ing, this keyword should be put before the C++ operator. Operator over­load­ing in C++ will behave like function over­load­ing.

Tip

Webspace hosting from IONOS is perfect for backing up your C++ projects online. You can install it with just one click.

An overview of C++ operators

Arith­met­ic operators

You probably already know the arith­met­ic C++ operators from school. They operate on numbers and return a new number. The arith­met­ic operators are all binary operators, except for the unary plus and unary minus.

C++ operator Meaning Example
+ Addition / unary plus 6 + 4
- Sub­trac­tion / unary minus 10 - 6
* Mul­ti­plic­a­tion 10* 3
/ Integer division 20 / 10
% Modulo 21 % 2

As­sign­ment operators

Similar to other pro­gram­ming languages, values are stored in variables. You need special operators to assign concrete values to these variables.

Simple as­sign­ment operators

C++ operator Meaning Example
= Simple as­sign­ment x = 3
++ Increment x++
-- Decrement x--

Combined as­sign­ment operators

In addition to the simple as­sign­ment operators, C++ also supports combined operators. These are arith­met­ic or bitwise op­er­a­tions which are sim­ul­tan­eously combined with a value as­sign­ment:

int x = 4;
x += 2;
cpp

The code example above shows that the variable x assigned the numeric value 4 with a simple as­sign­ment. The combined as­sign­ment operator ‘+=’ is used to perform an arith­met­ic addition after this and saves the resulting value directly in x. The as­sign­ment would be ‘x = x + 2’ after being written out.

C++ operator Meaning Example
+= Addition and as­sign­ment x += 2
-= Sub­trac­tion and as­sign­ment x -= 2
*= Mul­ti­plic­a­tion and as­sign­ment x* = 2
/= Division and as­sign­ment x /= 2
%= Modulo and as­sign­ment x %= 2
&= Bitwise And and as­sign­ment b &= 1
<<= Bitshift left and as­sign­ment b <<= 1
>>= Bitshift right and as­sign­ment b >>= 1
^= Bitwise Xor and as­sign­ment b ^= 1
` =` Bitwise Or and as­sign­ment

Logical C++ operators

You can use the C++ logical operators for pro­pos­i­tion­al com­par­is­ons of two ex­pres­sions. Logical operators are binary, except for the Logical Not, which only refers to one statement and negates its truth value.

C++ operator Meaning Example
&& Logical And true && true
` `
! Logical Not !true

Com­par­is­on operators

Com­par­is­on operators are C++ operators that examine the re­la­tion­ship between two elements. They are binary, except for the three-way com­par­is­on, which returns a number. The return value of all C++ com­par­is­on operators is a truth value.

C++ operator Meaning Example
== Equality a == b
!= In­equal­ity a != b
<= Smaller or equal a <= b
>= Greater or equal a >= b
< Smaller a < b
> Larger a > b
<=> Three-way com­par­is­on a <=> b

Bit ma­nip­u­la­tion

Bitwise C++ operators access in­di­vidu­al bits ef­fi­ciently and improve the speed of programs. They are es­pe­cially important for per­form­ance-oriented pro­gram­ming.

C++ operator Meaning Example
& Bitwise And a & b
` ` Bitwise Or
^ Bitwise Xor a ^ b
~ Bitwise negation ~a
<< Left shift a << b
>> Right shift a >> b

Memory man­age­ment

C++ is a machine-oriented language and has a several operators for memory man­age­ment.

C++ operator Meaning Example
& Address de­term­in­a­tion &x
sizeof() De­term­ines the memory re­quire­ment of an ex­pres­sion sizeof(x)
new Creates a new object and returns pointer object* pointer = new object()
delete Destroys an object delete object

Data access for objects and pointers

The following C++ operators will help you access in­di­vidu­al members of objects or the memory areas of pointers.

of pointers.

C++ operator Meaning Example
* Derefer­en­cing pointers, returns memory area *pointer = 3;
. Accesses members of an object object.member = 2;
-> Accesses members of an object with a pointer objectpointer->member = 2;
Go to Main Menu