Operators

Operators fall into broad classifications according to their primary use.

Assignment operators

=, +=, *=, ...

Arithmetic operators

+, -, *, /, %

Relational operators

<, <=, >, >=, ==, !=

Logical operators

&&, ||, !

Bitwise operators

&, |, ^, ~, <<, >>

Memory management operators

new, delete, sizeof

Pointer and access operators

*, &, ., ->, []

Scope resolution operators

::

Miscellaneous operators

conditional (?:), comma (,)

Operators have predefined meanings for built-in types, but not all operators are defined for all built-in types.

Operator Characteristics

Operators have the following special characteristics:

  • Precedence
  • Associativity
  • Number of required operands

Table 19.1 lists all the C++ operators and their characteristics, grouped by precedence and purpose, with groups of highest precedence listed first.

  • The Operands column contains the number of operands that the operator requires.
  • The Description column contains the conventional meaning of the operator for built-in types.
  • The Assoc column indicates the associativity that governs how an expression is evaluated if the same operator occurs more than once.

    • L indicates left-to-right associativity. Example:

      d = a + b + c; // a+b is evaluated first, then (a+b)+c
      

      assignment is evaluated last because of lower precedence.

    • R indicates right-to-left associativity:

      c = b = a; // a is assigned to b, then to c.
      
  • The Ovl column indicates whether or not the operator may be overloaded (redefined) for custom types.
  • The possible values for that column are:

    • Y: This operator can be overloaded as a global or member function.
    • M: This operator can be overloaded only as a class member function.
    • N: This operator cannot be overloaded.

Table 19.1. C++ Operators

Operator

Operands

Description

Example

Assoc

Ovl

::

one

Global Scope Resolution

:: name

R

N

::

two

Class/namespace scope resolution

className::memberName

L

N

->

two

Member selector via ptr

ptr->memberName

L

N

.

two

Member selector via obj

obj.memberName

L

N

->

one

Smart ptr

obj->member

R

M

[ ]

two

Subscript operator

ptr[expr]

L

M

( )

any[a]

Function call

function(argList)

L

N

( )

any

Value construction

className(argList)

L

M

++

one

Post increment

varName++

R

Y

--

one

Post decrement

varName--

R

Y

typeid

one

Type identification

typeid(type) or typeid(expr)

R

N

dynamic_cast

two

runtime checked conv

dynamic_cast<type>(expr)

L

N

static_cast

two

compile time checked conv

static_cast<type>(expr)

L

N

reinterpret_cast

two

unchecked conv

reinterpret_cast<type>(expr)

L

N

const_cast

two

const conv

const_cast<type>(expr)

L

N

sizeof

one

Size in bytes

sizeof expr or sizeof(type)

R

N

++

one

Pre Increment

++varName

R

Y

--

one

Pre Decrement

--varName

R

Y

~

one

Bitwise negation

~ expr

R

Y

!

one

Logical negation

! expr

R

Y

+, -

one

Unary plus, unary minus

+expr or -expr

R

Y

*

one

Pointer dereference

* ptr

R

Y

&

one

Address-of

& lvalue

R

Y

new

one

Allocate

new type or new type(expr-list)

R

Y

new [ ]

two

Allocate array

new type [ size ]

L

Y

delete

one

Deallocate

delete ptr

R

Y

delete [ ]

one

Deallocate array

delete [ ] ptr

R

M

( )

two

C-style type cast

( type ) expr

R

N[b]

->*

two

Member ptr selector via ptr

ptr->*ptrToMember

L

M

.*

two

Member ptr selector via obj

obj.*ptrToMember

L

N

*

two

Multiply

expr1 * expr2

L

Y

/

two

Divide

expr1 / expr2

L

Y

%

two

Remainder

expr1 % expr2

L

Y

+

two

Add

expr1 + expr2

L

Y

-

two

Subtract

expr1 - expr2

L

Y

<<

two

Bitwise left shift

expr << shiftAmt

L

Y

>>

two

Bitwise right shift

expr >> shiftAmt

L

Y

<

two

Less than

expr1 < expr2

L

Y

<=

two

Less or equal

expr1 <= expr2

L

Y

>

two

Greater

expr1 > expr2

L

Y

>=

two

Greater or equal

expr1 >= expr2

L

Y

==

two

Equal[c]

expr1 == expr2

L

Y

!=

two

Not equal

expr1 != expr2

L

Y

&

two

Bitwise AND

expr1 & expr2

L

Y

^

two

Bitwise XOR (exclusive OR)

expr1 ^ e2

L

Y

|

two

Bitwise OR (inclusive OR)

expr1 | expr2

L

Y

&&

two

Logical AND

expr1 && expr2

L

Y

||

two

Logical OR

expr1 || expr2

L

Y

=

two

Assign

expr1 = expr2

R

Y

*=

two

Multiply and assign

expr1 *= expr2

R

Y

/=

two

Divide and assign

expr1 /= expr2

R

Y

%=

two

Modulo and assign

expr1 %= expr2

R

Y

+=

two

Add and assign

expr1 += expr2

R

Y

-=

two

Subtract and assign

expr1 -= expr2

R

Y

<<=

two

Left shift and assign

expr1 <<= expr2

R

Y

>>=

two

Right shift and assign

expr1 >>= expr2

R

Y

&=

two

And and assign

expr1 &= expr2

R

Y

|=

two

Inclusive or and assign

expr1 |= expr2

R

Y

*=

two

Exclusive or and assign

expr1 ^= expr2

R

Y

? :

three

Conditional expression

bool ? expr : expr

L

N

throw

one

Throw exception

throw expr

R

N

,

two

Sequential Evaluation (comma)

expr , expr

L

Y

[a] The function call operator may be declared to take any number of operands.

[b] The type-cast operator may use constructors or conversion operators to convert custom types.

[c] Note that for float and double, this operator should not be used, as it requires an "exact" match, which is architecture-dependent, and not always reliable.


Part I: Introduction to C++ and Qt 4

C++ Introduction

Classes

Introduction to Qt

Lists

Functions

Inheritance and Polymorphism

Part II: Higher-Level Programming

Libraries

Introduction to Design Patterns

QObject

Generics and Containers

Qt GUI Widgets

Concurrency

Validation and Regular Expressions

Parsing XML

Meta Objects, Properties, and Reflective Programming

More Design Patterns

Models and Views

Qt SQL Classes

Part III: C++ Language Reference

Types and Expressions

Scope and Storage Class

Statements and Control Structures

Memory Access

Chapter Summary

Inheritance in Detail

Miscellaneous Topics

Part IV: Programming Assignments

MP3 Jukebox Assignments

Part V: Appendices

MP3 Jukebox Assignments

Bibliography

MP3 Jukebox Assignments



An Introduction to Design Patterns in C++ with Qt 4
An Introduction to Design Patterns in C++ with Qt 4
ISBN: 0131879057
EAN: 2147483647
Year: 2004
Pages: 268

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net