Loading

Operators

Precedence Table


NameSymbol UsagePrecedenceAssociativityArity

Global Scope Resolution::giInt17RightUnary
Class Scope ResolutionCMyClass::tiInt17LeftBinary
Namespace Scope ResolutionNMyNamespace::tiInt17LeftBinary

DotqMyObj.miInt16LeftBinary
ArrowqpMyObjPtr->miInt16LeftBinary
SubscriptiaIntArray[uiIndex]16LeftBinary
Postfix IncrementiInt++16RightUnary
Postfix DecrementiInt--16RightUnary
Sizeofsizeof()16RightUnary

Prefix Increment++iInt15RightUnary
Prefix Decrement--iInt15RightUnary
Bitwise Not~uiInt15RightUnary
Logical Not!bBool15RightUnary
Negation-iInt15RightUnary
Unary Plus+iInt15RightUnary
Address Of&iInt15RightUnary
Dereference*ipIntPtr15RightUnary
Newnew CMyClass15RightUnary
Deletedelete qpMyObj15RightUnary
Type Conversion/Cast(double)iInt15RightUnary

Dot DereferenceqMyObj.*mipDataPtr14LeftBinary
Arrow DereferenceqpMyObjPtr->*mipDataPtr14LeftBinary

MultiplicationiInt1 * iInt213LeftBinary
DivisioniInt1 / iInt213LeftBinary
ModulusiInt1 % iInt213LeftBinary

AdditioniInt1 + iInt212LeftBinary
SubtractioniInt1 - iInt212LeftBinary

Shift LeftuiBits << uiShift11LeftBinary
Shift RightuiBits >> uiShift11LeftBinary

Less ThaniInt1 < iInt210LeftBinary
Less Than or EqualiInt1 <= iInt210LeftBinary
Greater ThaniInt1 > iInt210LeftBinary
Greater Than or EqualiInt1 >= iInt210LeftBinary

EqualiInt1 == iInt29LeftBinary
Not EqualiInt1 != iInt29LeftBinary

Bitwise AnduiBits1 & uiBits28LeftBinary

Bitwise Exclusive OruiBits1 ^ uiBits27LeftBinary

Bitwise OruiBits1 | uiBits26LeftBinary

Logical AndbBool1 && bBool25LeftBinary

Logical OrbBool1 || bBool24LeftBinary

ConditionalbBool ? iIntTrue : iIntFalse3LeftTernary

AssignmentiIntDest = iIntSrc2RightBinary
Multiplication AssignmentiIntDest *= iIntSrc2RightBinary
Division AssignmentiIntDest /= iIntSrc2RightBinary
Modulus AssignmentiIntDest %= iIntSrc2RightBinary
Addition AssignmentiIntDest += iIntSrc2RightBinary
Subtraction AssignmentiIntDest -= iIntSrc2RightBinary
Shift Left AssignmentiIntDest <<= iIntSrc2RightBinary
Shift Right AssignmentiIntDest >>= iIntSrc2RightBinary
And AssignmentuiBitsDest &= uiBitsSrc2RightBinary
Or AssignmentuiBitsDest |= uiBitsSrc2RightBinary
Exclusive Or AssignmentuiBitsDest ^= uiBitsSrc2RightBinary

CommaiIntArg1, iIntArg21LeftBinary






Increment and Decrement Operators    ++, --


Example 1

// Prefix increment: increment then output 1
int iX = 0;
cout << ++iX << endl;

Example 2

// Postfix increment: output 0 then increment
int iX = 0;
cout << iX++ << endl;

Example 3

// Prefix decrement: decrement then output -1
int iX = 0;
cout << --iX << endl;

Example 4

// Postfix decrement: output 0 then decrement
int iX = 0;
cout << iX-- << endl;

Arithmetical Operators    +, -, *, /, %


Example 1

// Addition
int iSum = 1 + 2;

Example 2

// Subtraction
int iDifference = 2 - 1;

Example 3

// Multiplication
int iProduct = 3 * 2;

Example 4

// Division
double dQuotient = 4.0 / 3.0;

Example 5

// Modulus
int iRemainder = 10 % 3;

Logical Operators    &&, ||, !


Example 1

// And
bool bAnd = (bIsFirst && bIsFull);

Example 2

// Or
bool bOr = (bIsLast || bIsEmpty);

Example 3

// Not
bool bNot = !bDone;

Relational Operators    <, >, ==, <=, >=, !=


Example 1

// Less than
if (iX < iY) {
    // Some code
}

Example 2

// Greater than
if (iX > 10) {
    // Some code
}

Example 3

// Equal to
if (4 == iY) {
    // Some code
}

Example 4

// Less than or equal to
if (8.1 <= dZ) {
    // Some code
}

Example 5

// Greater than or equal to
if (dX  >= dZ) {
    // Some code
}

Example 6

// Not equal to
if (dX  != 10.5) {
    // Some code
}

Bitwise Operators    &, |, ^, ~


Example 1

// Use a mask and bitwise and to extract least two bits of uiX
unsigned int uiAnd = (uiX & 3);

Example 2

// Use the lowest byte mask and bitwise or to ignore the last byte
unsigned int uiMatchIP = (uiAddressIP | 255);

Example 3

// Use bitwise exclusive or to swap without a third variable
unsigned int uiA = 46;
unsigned int uiB = 73;
// This swaps the values of uiA and uiB
uiA = uiA ^ uiB;
uiB = uiA ^ uiB;
uiA = uiA ^ uiB;

Example 4

// Use the bitwise not to get the photo negative of a pixel
unsigned int uiNegative = ~uiPixelColor;

Shift Operators    >>, <<


Example 1

// Use the right shift to divide by two
unsigned int uiHalfX = (uiX >> 1);

Example 2

// Use the left shift to multiply by eight
unsigned int uiEightX = (uiX << 3);

Assignment Operators    =, +=, -=, *=, /=, %=, >>=, <<=, &=, |=, ^=


Example 1

// Simple assignment
uiX = uiY;

Example 2

// Add uiY to uiX
uiX += uiY;

Example 3

// Subtract uiY from uiX
uiX -= uiY;

Example 4

// Multiply uiX by uiY
uiX *= uiY;

Example 5

// Divide uiX by uiY
uiX /= uiY;

Example 6

// Take the modulus of uiX with respect to 2
uiX %= 2;

Example 7

// Divide uiX by 4
uiX >>= 2;

Example 8

// Multiply uiX by 16
uiX <<= 4;

Example 9

// Keep the last 8 bits of uiX
uiX &= 255;

Example 10

// Turn on the last 2 bits of uiX
uiX |= 3;

Example 11

// Alternate the last 4 bits of uiX
uiX ^= 15;

Conditional Operator    ? :


Example 1

// Simple if-then
unsigned int uiX = (uiZ > 5) ? 5 : 10;

Address Of Operator    &


Example 1

// Simple pointer assignment
double*dpX = &dX;

Dereference Operator    *


Example 1

// Assign dX the value of dY via the pointer dpX
double*dpX = &dX;
*dpX = dY;

Comma Operator    ,


Example 1

// The variable iX takes on values 0 to 19 and
// iY takes on the corresponding powers of 2
int iX;
int iY;
for (iX = 0, iY = 1; iX < 20; ++iX, iY * = 2) {
    // Some code
}

Example 2

// Pass multiple arguments to a function
MyFunction(iX, iY);

Negation and Unary Plus Operators    -, +


Example 1

// Assign iNegX the value of iX with the opposite sign
int iNegX = -iX;

Example 2

// The unary plus does not affect anything. This sets iX equal to iY
int iX = +iY;

Dot Operator    .


Example 1

// Call a member function of an object with two double arguments
CMyClass qMyObj;

qMyObj.MyFunction(10.9, 8.3);

Arrow Operator    ->


Example 1

// Set the value of a data member of an object
CMyClass* qpMyObj = new CMyClass;

QpMyObj->miX = iY;

Dot Dereference Operator    .*


Example 1

// Declare and initialize a member data pointer
double CMyClass::*mdpDataPtr = &CMyClass::mdX;

CMyClass qMyObj;

// Assign qMyObj.mdX the value 10.0 via the member data pointer
qMyObj.*mdpDataPtr = 10.0;

Example 2

// Declare and initialize a member function pointer
void (CMyClass::mpfnFnPtr)(double) = &CMyClass::SetX;

CMyClass qMyObj;

// Call qMyObj.SetX(15.2) via the member function pointer
qMyObj.*mpfnFnPtr(15.2);

Arrow Dereference Operator    ->*


Example 1

// Declare and initialize a member data pointer
double CMyClass::*mdpDataPtr = &CMyClass::mdX;

CMyClass* qpMyObj = &qSomeObject;

// Assign qpMyObj->mdX the value 10.0 via the member data pointer
qpMyObj->*mdpDataPtr = 10.0;

Example 2

// Declare and initialize a member function pointer
void (CMyClass::mpfnFnPtr)(double) = &CMyClass::SetX;

CMyClass* qpMyObj = &qSomeObject;

// Call qpMyObj->SetX(15.2) via the member function pointer
qpMyObj->*mpfnFnPtr(15.2);

Scope Resolution Operator    ::


Example 1

// Use the class scope resolution operator to access the class function
CMyClass::MyFunction(3.6);

Example 2

// Use the namespace scope resolution operator to assign variable a value
NMyNamespace::mdX = 3.6;

Example 3

// Use the global scope resolution operator to break out of local scope
::MyFunction(3.6);

Type Conversion/Cast Operator    ()


Example 1

// Type Conversion from a double to an int—truncates to 14
double dX = 14.7;

int iX = (int)dX;

Example 2

// Type cast from an unsigned int to
// an unsigned char—takes lowest byte = 252 or 0xFC
unsigned int uiInt          = 0xFFFEFDFC;

unsigned int* uipIntPtr     = &uiInt;

unsigned char* ucpLowByte   = (unsigned char*)uipIntPtr;

Subscript Operator    [ ]


Example 1

// Access an element of an array to set a char to 'X'
char caName[]   = "XoaX.net";

char cX         = caName[3];

Sizeof Operator    sizeof()


Example 1

// Get the size in bytes of a data instance
int iInt = 10;

unsigned int uiIntSize = sizeof(iInt);

Example 2

// Get the size in bytes of a data type
unsigned int uiDblSize = sizeof(double);

Example 3

// Get the number of elements in an array
// Then divide the array size by the element size
double  daArray[] = {3.4, 8.1, 7.4};

unsigned int uiArraySize = sizeof(daArray) / sizeof(daArray[0]);

New and Delete Operators    new, delete


Example 1

// Dynamically allocate and deallocate a single variable
int* ipX = new int;

// Some code
delete ipX;

Example 2

// Dynamically allocate and deallocate an array
double* dpVector = new int[10];

// Some code
delete []dpVector;

Example 3

// Allocate an array of function pointers,
// which take no arguments and return an int
int *fnpaFnPtrArray = new (int (*[5])());

// Some code

// Deallocate the array
delete [] fnpaFnPtrArray;