What are the most important C++ operators?

C++ operators are essential for programming with C++. Whether you are just beginning to learn C++ or have been working with the programming language for a while, operators can make programming 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 arithmetic operators you learn in school, for example, ‘+’ for addition and ‘-‘ for subtraction.

C++ operators are not only distinguishable according to their functional 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 precedence?

As with arithmetic operators in school or Python operators, there is an operator precedence for C++ operators. This specifies the order in which the operators should be evaluated. The dot before dash rule applies for arithmetic operators, however, there are other rules for other C++ operators.

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

The example above shows the logical expression being evaluated after the if operator. The && operator (logical And) has priority over the || operator (logical Or). So, if the evaluation of ‘var1 && var2’ or the evaluation 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 overloading, this keyword should be put before the C++ operator. Operator overloading in C++ will behave like function overloading.

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

Arithmetic operators

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

C++ operator Meaning Example
+ Addition / unary plus 6 + 4
- Subtraction / unary minus 10 - 6
* Multiplication 10* 3
/ Integer division 20 / 10
% Modulo 21 % 2

Assignment operators

Similar to other programming languages, values are stored in variables. You need special operators to assign concrete values to these variables.

Simple assignment operators

C++ operator Meaning Example
= Simple assignment x = 3
++ Increment x++
-- Decrement x--

Combined assignment operators

In addition to the simple assignment operators, C++ also supports combined operators. These are arithmetic or bitwise operations which are simultaneously combined with a value assignment:

int x = 4;
x += 2;
cpp

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

C++ operator Meaning Example
+= Addition and assignment x += 2
-= Subtraction and assignment x -= 2
*= Multiplication and assignment x* = 2
/= Division and assignment x /= 2
%= Modulo and assignment x %= 2
&= Bitwise And and assignment b &= 1
<<= Bitshift left and assignment b <<= 1
>>= Bitshift right and assignment b >>= 1
^= Bitwise Xor and assignment b ^= 1
` =` Bitwise Or and assignment

Logical C++ operators

You can use the C++ logical operators for propositional comparisons of two expressions. 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

Comparison operators

Comparison operators are C++ operators that examine the relationship between two elements. They are binary, except for the three-way comparison, which returns a number. The return value of all C++ comparison operators is a truth value.

C++ operator Meaning Example
== Equality a == b
!= Inequality a != b
<= Smaller or equal a <= b
>= Greater or equal a >= b
< Smaller a < b
> Larger a > b
<=> Three-way comparison a <=> b

Bit manipulation

Bitwise C++ operators access individual bits efficiently and improve the speed of programs. They are especially important for performance-oriented programming.

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 management

C++ is a machine-oriented language and has a several operators for memory management.

C++ operator Meaning Example
& Address determination &x
sizeof() Determines the memory requirement of an expression 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 individual members of objects or the memory areas of pointers.

of pointers.

C++ operator Meaning Example
* Dereferencing 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;
Was this article helpful?
Page top