|
|
int
) are applied the addition operator, and then the assignment operator. For a fundamental arithmetic type, the meaning of such operations is generally obvious and unambiguous, but it may not be so for certain class types. For example:
|
|
b
and c
does. In fact, this code alone would cause a compilation error, since the type myclass
has no defined behavior for additions. However, C++ allows most operators to be overloaded so that their behavior can be defined for just about any type, including classes. Here is a list of all the operators that can be overloaded:Overloadable operators |
---|
|
operator
functions, which are regular functions with special names: their name begins by the operator
keyword followed by the operator sign that is overloaded. The syntax is:type operator sign (parameters) { /*... body ...*/ }
x
and y
. The addition operation of two cartesian vectors is defined as the addition both x
coordinates together, and both y
coordinates together. For example, adding the cartesian vectors (3,1)
and (1,2)
together would result in (3+1,1+2) = (4,3)
. This could be implemented in C++ with the following code:
|
|
4,3 |
CVector
, consider that some of them refer to the class name (i.e., the type) CVector
and some others are functions with that name (i.e., constructors, which must have the same name as the class). For example:
|
|
operator+
of class CVector
overloads the addition operator (+
) for that type. Once declared, this function can be called either implicitly using the operator, or explicitly using its functional name:
|
|
operator+
to actually subtract or that overloads operator==
to fill the object with zeros, is perfectly valid, although using such a class could be challenging.operator+
is naturally the operand to the right hand side of the operator. This is common to all binary operators (those with an operand to its left and one operand to its right). But operators can come in diverse forms. Here you have a table with a summary of the parameters needed for each of the different operators than can be overloaded (please, replace @
by the operator in each case):Expression | Operator | Member function | Non-member function |
---|---|---|---|
@a | + - * & ! ~ ++ -- | A::operator@() | operator@(A) |
a@ | ++ -- | A::operator@(int) | operator@(A,int) |
a@b | + - * / % ^ & | < > == != <= >= << >> && || , | A::operator@(B) | operator@(A,B) |
a@b | = += -= *= /= %= ^= &= |= <<= >>= [] | A::operator@(B) | - |
a(b,c...) | () | A::operator()(B,C...) | - |
a->b | -> | A::operator->() | - |
(TYPE) a | TYPE | A::operator TYPE() | - |
a
is an object of class A
, b
is an object of class B
and c
is an object of class C
. TYPE
is just any type (that operators overloads the conversion to type TYPE
).operator+
. But some operators can also be overloaded as non-member functions; In this case, the operator function takes an object of the proper class as first argument.
|
|
4,3 |
this
represents a pointer to the object whose member function is being executed. It is used within a class's member function to refer to the object itself.
|
|
yes, &a is b |
operator=
member functions that return objects by reference. Following with the examples on cartesian vector seen before, its operator=
function could have been defined as:
|
|
operator=
.
|
|
6 7 |
|
|
|
|
n
within class Dummy
shared by all objects of this class.this
.const
object:
|
|
const
for those accessing them from outside the class. Note though, that the constructor is still called and is allowed to initialize and modify these data members:
|
|
10 |
const
object can only be called if they are themselves specified as const
members; in the example above, member get
(which is not specified as const
) cannot be called from foo
. To specify that a member is a const
member, the const
keyword shall follow the function prototype, after the closing parenthesis for its parameters:
|
|
const
can be used to qualify the type returned by a member function. This const
is not the same as the one which specifies a member as const
. Both are independent and are located at different places in the function prototype:
|
|
const
cannot modify non-static data members nor call other non-const
member functions. In essence, const
members shall not modify the state of an object.const
objects are limited to access only member functions marked as const
, but non-const
objects are not restricted and thus can access both const
and non-const
member functions alike.const
objects, and thus marking all members that don't modify the object as const is not worth the effort, but const objects are actually very common. Most functions taking classes as parameters actually take them by const
reference, and thus, these functions can only access their const
members:
|
|
10 |
get
was not specified as a const
member, the call to arg.get()
in the print
function would not be possible, because const
objects only have access to const
member functions.const
and the other is not: in this case, the const
version is called only when the object is itself const, and the non-const
version is called when the object is itself non-const
.
|
|
15 20 |
|
|
int
with the values 115 and 36 we would write:
|
|
|
|
template <...>
prefix:
|
|
100 |
getmax
:
|
|
T
's? There are three T
's in this declaration: The first one is the template parameter. The second T
refers to the type returned by the function. And the third T
(the one between angle brackets) is also a requirement: It specifies that this function's template parameter is also the class template parameter.mycontainer
that can store one element of any type and that has just one member function called increase
, which increases its value. But we find that when it stores an element of type char
it would be more convenient to have a completely different implementation with a function member uppercase
, so we decide to declare a class template specialization for that type:
|
|
8 J |
|
|
template<>
, including an empty parameter list. This is because all types are known and no template arguments are required for this specialization, but still, it is the specialization of a class template, and thus it requires to be noted as such.<char>
specialization parameter after the class template name. This specialization parameter itself identifies the type for which the template class is being specialized (char
). Notice the differences between the generic class template and the specialization:
|
|
Previous: Classes (I) | Index | Next: Special members |