delete and delete[] — destroying dynamic objects
A delete expression destroys a single object created by new, while delete[] destroys an array created by new[]. Choosing the matching form is mandatory; mixing the forms causes undefined behavior.
CKTIS / C++
Programming syntax, operators and concepts. Find an explanation, explore an example and return to learning.
A delete expression destroys a single object created by new, while delete[] destroys an array created by new[]. Choosing the matching form is mandatory; mixing the forms causes undefined behavior.
The unary * operator denotes the object or function addressed by a pointer. It enables reading and, when the type permits, modifying the pointed-to object; it does not create a copy.
The / operator calculates a quotient. For integer operands it discards the fractional part toward zero; a floating-point operand can change the calculation type. Convert before dividing to preserve a fractional result.
The /= operator divides the left operand's value by the right operand and stores the result back. Operand types determine the kind of division, and storing the result may require another conversion. The left operand is evaluated only once.
dynamic_cast converts pointers and references within class hierarchies, checking conversions at runtime where required. A failed pointer cast returns nullptr, whereas a failed reference cast throws std::bad_cast.
The == operator tests operands for equality. Built-in comparison returns bool, while classes can define comparison through operator==. Do not confuse == with assignment = or pointer comparison with comparison of the pointed-to values.
Parentheses () following an expression form a call with the supplied arguments. This syntax calls functions and callable objects; a class can provide it through an operator() member function.
The >= operator checks whether the left operand is greater than or equal to the right operand. Built-in comparisons return bool. Numeric comparisons require attention to type conversions, particularly when mixing signed and unsigned integers.
The > operator tests whether the left operand is greater than the right. A built-in comparison returns bool; equal values produce false. Learn about numeric conversions, chained-comparison pitfalls, and the distinction between built-in and overloaded comparisons.
The ++ operator increments its operand. For built-in types, ++x provides the modified object, whereas x++ produces a copy of the previous value. Learn the operand requirements, the distinction between the two forms, and their basic restrictions.
The != operator tests whether its operands are unequal. The built-in operation returns bool after required conversions. Classes can define operator!= or use an eligible operator== through comparison rewriting rules.
The built-in << operator shifts an integer value's bits left, filling vacated positions with zeros. The shift count must be within the width of the promoted left operand. Do not confuse it with stream insertion.
The <<= operator shifts the left operand's bits to the left and stores the result in the same object. The left operand is evaluated only once, and the valid shift-count range depends on its type after integral promotion.
The <= operator checks whether its left operand is less than or equal to its right operand. The built-in version returns bool. It is useful for inclusive limits; take care with type conversions and chained comparisons.
The < operator tests whether its left operand is less than its right operand. A built-in comparison returns bool and excludes equality. Watch for type conversions, restrictions on pointer comparisons, and the misleading chained expression a < b < c.
The built-in && operator produces true only when both conditions are true. It evaluates the left operand first and skips the right operand when the left is false; this is short-circuit evaluation.
The unary operator ! reverses its operand's truth value. The built-in operator contextually converts the operand to bool and returns bool: true for a false value and false for a true value.
The built-in || operator returns bool: true when at least one operand is true, and false otherwise. It short-circuits: when the left operand is true, the right operand is not evaluated.
The . operator accesses a member of a class, struct, or union object. It lets you read and modify data members and call member functions. It also works through an object reference, but does not replace -> for a pointer.
The *= operator multiplies the left operand’s current value by the right operand and stores the result back. The left operand is evaluated only once, and converting the result to its type can discard a fractional part.
The binary * operator calculates the product of two values. The result type depends on the operand types and the usual arithmetic conversions, not on the destination variable's type. Learn multiplication precedence and the pitfalls of overflow.
A new-expression creates an object and returns a pointer to it; it usually also obtains storage. Distinguish it from the allocation function operator new, and learn about array new[] and the Placement new-expression that constructs an object in supplied storage.
The -> operator accesses a data member or calls a member function of the object a pointer points to. For the built-in operator, pointer->member is equivalent to (*pointer).member.
A pointer to member identifies a class member, not a particular object's member. Form it with &T::member, then apply it to an object with .* or to an object pointer with ->*.