c++oggetti
TRANSCRIPT
-
8/7/2019 c++oggetti
1/17
1
Operator name Syntax Overloadable Included
in C
Basic assignment a = b Yes Yes
Addition a + b Yes Yes
Subtraction a - b Yes Yes
Unary plus (integer
promotion)
+a Yes Yes
Unary minus
(additive inverse)
-a Yes Yes
Multiplication a * b Yes Yes
Division a / b Yes Yes
Modulo (remainder) a % b Yes Yes
Increment Prefix ++a Yes Yes
Suffix a++ Yes Yes
Decrement Prefix --a Yes Yes
Suffix a-- Yes Yes
-
8/7/2019 c++oggetti
2/17
2
Prototype examples (T is any type)
As member of T Outside class definitions
T& T::operator =(const T& b); N/A
T T::operator +(const T& b) const; T operator +(const T& a, const T& b);
T T::operator -(const T& b) const; T operator -(const T& a, const T& b);
T T::operator +() const; T operator +(const T& a);
T T::operator -() const; T operator -(const T& a);
T T::operator *(const T& b) const; T operator *(const T &a, const T& b);
T T::operator /(const T& b) const; T operator /(const T& a, const T& b);
T T::operator %(const T& b) const; T operator %(const T& a, const T& b);
T& T::operator ++(); T& operator ++(T& a);
T T::operator ++(int); T operator ++(T& a, int);
Note: C++ uses the unnamed dummy-parameter int to
differentiate between prefix and Suffix increment operators.
T& T::operator --(); T& operator --(T& a);T T::operator --(int); T operator --(T& a, int);
Note: C++ uses the unnamed dummy-parameter int to
differentiate between prefix and Suffix decrement operators.
-
8/7/2019 c++oggetti
3/17
1
Operator name Syntax Overloadable Included
in C
Equal to a == b Yes Yes
Not equal to a != b Yes Yes
Greater than a > b Yes Yes
Less than a < b Yes Yes
Greater than or equal to a >= b Yes Yes
Less than or equal to a
-
8/7/2019 c++oggetti
4/17
2
Prototype examples (T is any type)
As member of T
bool T::operator ==(const T& b) const;
bool T::operator !=(const T& b) const;
bool T::operator >(const T& b) const;
bool T::operator =(const T& b) const;
bool T::operator
-
8/7/2019 c++oggetti
5/17
3
Outside class definitions
bool operator ==(const T& a, const T& b);
bool operator !=(const T& a, const T& b);
bool operator >(const T& a, const T& b);
bool operator =(const T& a, const T& b);
bool operator
-
8/7/2019 c++oggetti
6/17
1
Operator name Syntax Overloadable Included
in C
Logical negation (NOT) !a Yes Yes
Logical AND a && b Yes Yes
Logical OR a || b Yes Yes
-
8/7/2019 c++oggetti
7/17
2
Prototype examples (T is any type)
As member of T
bool T::operator !() const;
bool T::operator &&(const T& b) const;
bool T::operator ||(const T& b) const;
-
8/7/2019 c++oggetti
8/17
3
Outside class definitions
bool operator !(const T& a);
bool operator &&(const T& a, const T& b);
bool operator ||(const T& a, const T& b);
-
8/7/2019 c++oggetti
9/17
1
Operator name Syntax Overloadable Included
in C
Bitwise NOT ~a Yes Yes
Bitwise AND a & b Yes Yes
Bitwise OR a | b Yes Yes
Bitwise XOR a ^ b Yes Yes
Bitwise left shift[note 1] a > b Yes Yes
-
8/7/2019 c++oggetti
10/17
2
Prototype examples (T is any type)
As member of T
T T::operator ~() const;
T T::operator &(const T& b) const;
T T::operator |(const T& b) const;
T T::operator ^(const T& b) const;
T T::operator (const T& b) const;
-
8/7/2019 c++oggetti
11/17
3
Outside class definitions
T operator ~(const T& a);
T operator &(const T& a, const T& b);
T operator |(const T& a, const T& b);
T operator ^(const T& a, const T& b);
T operator (const T& a, const T& b);
-
8/7/2019 c++oggetti
12/17
1
Operator name Syntax Overloadable Included
in C
Addition assignment a += b Yes Yes
Subtraction assignment a -= b Yes Yes
Multiplication assignment a *= b Yes Yes
Division assignment a /= b Yes Yes
Modulo assignment a %= b Yes Yes
Bitwise AND assignment a &= b Yes Yes
Bitwise OR assignment a |= b Yes Yes
Bitwise XOR assignment a ^= b Yes Yes
Bitwise left shift assignment a = b Yes Yes
-
8/7/2019 c++oggetti
13/17
2
Prototype examples (T is any type)
As member of T Outside class definitions
T& T::operator +=(const T& b); T& operator +=(T& a, const T& b);
T& T::operator -=(const T& b); T& operator -=(T& a, const T& b);
T& T::operator *=(const T& b); T& operator *=(T& a, const T& b);
T& T::operator /=(const T& b); T& operator /=(T& a, const T& b);
T& T::operator %=(const T& b); T& operator %=(T& a, const T& b);
T& T::operator &=(const T& b); T& operator &=(T& a, const T& b);
T& T::operator |=(const T& b); T& operator |=(T& a, const T& b);
T& T::operator ^=(const T& b); T& operator ^=(T& a, const T& b);
T& T::operator =(T& a, const T& b);
-
8/7/2019 c++oggetti
14/17
1
Operator name Syntax Overloadable Included
in C
Array subscript a[b] Yes Yes
Indirection ("variable pointed to by a") *a Yes Yes
Reference ("address of a") &a Yes Yes
Member b of object pointed to by a a->b Yes Yes
Member b of object a a.b No Yes
Member pointed to by b of object pointed to by a, a->*b Yes No
Member pointed to by b of object a a.*b No No
-
8/7/2019 c++oggetti
15/17
2
Prototype examples
As member of T Outside class definitions
R& T::operator [](const T2& b); N/A
R& T::operator *(); R& operator *(T& a);
T* T::operator &(); T* operator &(T& a);
T2* T::operator ->(); N/A
N/A
R T::operator->*(T2); R operator->*(T, T2);
N/A
-
8/7/2019 c++oggetti
16/17
1
Operator name Syntax Overloadable Included
in C
Function call
See Function object.
a(a1, a2) Yes Yes
Comma a, b Yes Yes
Ternary conditional a ? b : c No Yes
Scope resolution a::b No No
Size-of sizeof(a)
sizeof(type)
No Yes
Type identification typeid(a)
typeid(type)
No No
Cast (type) a Yes Yes
Allocate storage new type Yes No
Allocate storage (array) new type[n] Yes No
Deallocate storage delete a Yes NoDeallocate storage (array) delete[] a Yes No
-
8/7/2019 c++oggetti
17/17
2
Prototype examples
As member of T Outside class definitions
R T::operator ()(Arg1 a1, Arg2 a2, ); N/A
T2& T::operator ,(T2& b) const; T2& operator ,(const T& a, T2& b);
N/A
N/A
N/A
N/A
T::operator T2() const; N/A
Note: for user-defined conversions, the return type implicitly and
necessarily matches the operator name.
void* T::operator new(size_t x); void* operator new(size_t x);
void* T::operator new[](size_t x); void* operator new[](size_t x);
void T::operator delete(void* x); void operator delete(void* x);void T::operator delete[](void* x); void operator delete[](void* x);