Binary Operators Summary (C++)

From Appmethod Topics
Jump to: navigation, search

Go Up to Binary Operators Index


These are the binary operators in Appmethod C++:

Arithmetic Operators

For more information, see Arithmetic Operators (Unary).

Arithmetic Operator Description Arithmetic Operator Description

+

Binary plus (add)

-

Binary minus (subtract)

*

Multiply

/

Divide

%

Remainder (modulus)

Bitwise Operators

For more information, see Bitwise Operators.

Bitwise Operator Description Bitwise Operator   Description

<<

Shift left

>>

Shift right

&

Bitwise AND

^

Bitwise XOR (exclusive OR)

|

Bitwise inclusive OR

Logical Operators

For more information, see Logical Operators.

Logical Operator Description

&&

Logical AND

Assignment Operators

For more information, see:

Assignment Operator Description Assignment Operator Description

=

Assignment

*=

Assign product

/=

Assign quotient

%=

Assign remainder (modulus)

+=

Assign sum

-=

Assign difference

<<=

Assign left shift

>>=

Assign right shift

&=

Assign bitwise AND

^=

Assign bitwise XOR

|=

Assign bitwise OR

Relational Operators

For more information, see:

Relational Operator Description Relational Operator Description

<

Less than

>

Greater than

<=

Less than or equal to

>=

Greater than or equal to

==

Equal to

!=

Not equal to

Component Selection Operators

For more information, see Overloading the Class Member Access Operator ->.

Component Selection Operator Description

.

Direct component selector

->

Indirect component selector

Class Member Operators

For more information, see:

Class Member Operator Description

::

Scope access/resolution

Use the scope access (or resolution) operator :: (two colons) to access a global (or file duration) name even if it is hidden by a local redeclaration of that name.

.*

Dereference pointer to class member

Use the .* operator to dereference pointers to class members.

The first operand must be a class type. If the type of the first operand is class type TFoo, or is a class that has been derived from class type TFoo, the second operand must be a pointer to a member of a class type TFoo.

->*

Dereference pointer to class member

Use the ->* operator to dereference pointers to class members.

The first operand must be a pointer to a class type. If the type of the first operand is a pointer to class type TFoo, or is a pointer to a class derived from class type TFoo, the second operand must be a pointer to a member of class type TFoo.

Example

The pointer-to-member operators are probably best explained with an example such as this one:

#include <iostream>

class TFoo {
public:
  void func() {
    std::cout << __func__ << std::endl;
  }
  int data;
};

void (TFoo::*pmfn)() = &TFoo::func;
int TFoo::*pmd = &TFoo::data;

int main() {
  TFoo foo;
  TFoo *pfoo = &foo;

  // Call func with foo/pfoo
  (foo.*pmfn)();   // With object
  (pfoo->*pmfn)(); // With pointer

  // Set/read data with object and ptr respectively
  foo.*pmd = 123;
  std::cout << "data=" << pfoo->*pmd << std::endl;

  return 0;
}

Conditional Operators

For more information, see Conditional Operators.

Conditional Operator Description

?:

Actually a ternary operator. For example: a ? x : y means "if a then x else y".

Comma Operator

For more information, see Comma Operator.

Comma Operator Description

,

Evaluate

Other C++ Specific Operators

See Also