C++ programming to remove the use of operators and equality operators

  • 2020-05-07 20:10:41
  • OfStack

delete delete operator
frees memory blocks.
grammar


[::] delete cast-expression
[::] delete [ ] cast-expression

note
The cast-expression parameter must be a pointer to a block of memory previously allocated to an object created using the new operator. The delete operator has a result type of void, so it does not return a value. Such as:


CDialog* MyDialog = new CDialog;
// use MyDialog
delete MyDialog;

Using delete for Pointers to Pointers that do not use new allocations will produce unpredictable results. However, you can use delete for Pointers with a value of 0. This setting means that when new fails and returns 0, deleting the results of the failed new operation does no harm.
The new and delete operators can also be used for built-in types (including arrays). If pointer refers to an array of 1, place an empty bracket before pointer:
int* set = new int[100];
//use set[]
delete operator delete [] set;
Using the delete operator on an object frees its memory. Programs that de-reference Pointers after deleting objects can have unpredictable results or crash.
When delete is used to free the memory of an C++ class object, the object's destructor (if the object has a destructor) is called before the object's memory is freed.
If the operator of the delete operator is a modifiable lvalue, its value is not defined after the object is deleted.
Using delete
There are two syntax variations: one for a single object and one for an array of objects. The following code snippet illustrates the differences between them:


// expre_Using_delete.cpp
struct UDType 
{
};

int main()
{
  // Allocate a user-defined object, UDObject, and an object
  // of type double on the free store using the
  // new operator.
  UDType *UDObject = new UDType;
  double *dObject = new double;
  // Delete the two objects.
  delete UDObject;
  delete dObject; 
  // Allocate an array of user-defined objects on the
  // free store using the new operator.
  UDType (*UDArr)[7] = new UDType[5][7];
  // Use the array syntax to delete the array of objects.
  delete [] UDArr;
}

Undefined results are generated in two cases: using the array form of delete in the object (delete []) and the non-array form of delete in the array.

The way delete works
The function operator delete is called.
For objects that are not of class type (class, struct, or union), the global delete operator is called. For objects of class type, the name of the release function is resolved in the global scope if the delete expression begins with the 1-yuan scope resolution operator (::). Otherwise, the delete operator will call the destructor for the object before freeing memory (if the pointer is not null). The delete operator can be defined for each class. If this definition does not exist for a given class, the global delete operator is called. If the delete expression is used to free a class object whose static object has a virtual destructor, the release function is resolved by a dynamically typed virtual destructor of the object.


equality operator: == and! =

grammar


   expression == expression
expression != expression

note
The 2-bit equality operator strictly compares the equality or inequality of its operands.
The equality operator (== =) instead of (! =)) has a lower priority than the relational operator, but it behaves similarly. The result type of these operators is bool.
If the two operands have the same value, the equality operator (==) returns true (1). Otherwise, false (0) is returned. If the operands do not have the same value, the inequality operator (! =) return true; Otherwise, false is returned.
! The operator keyword of =
The not_eq operator is! The text equivalent of =. There are two ways to access the not_eq operator in your program: include the header file iso646.h, or compile with the /Za (disabled language extension) compiler option.


// expre_Equality_Operators.cpp
// compile with: /EHsc
#include <iostream>

using namespace std;

int main() {
  cout << boolalpha
     << "The true expression 3 != 2 yields: "
     << (3 != 2) << endl
     << "The false expression 20 == 10 yields: "
     << (20 == 10) << endl;
}

The equality operator compares Pointers to members of the same type 1.


Related articles: