You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The function-call operator, when overloaded, does not modify how functions are called; rather, it modifies how the operator is to be interpreted when applied to objects of a given class type. For example, the following code would usually be meaningless:
28
28
29
-
```
29
+
```cpp
30
30
Point pt;
31
31
pt( 3, 2 );
32
32
```
33
33
34
34
Given an appropriate overloaded function-call operator, however, this syntax can be used to offset the `x` coordinate 3 units and the `y` coordinate 2 units. The following code shows such a definition:
Copy file name to clipboardExpand all lines: docs/cpp/function-call-operator-parens.md
+7-7Lines changed: 7 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,33 +30,33 @@ postfix-expression
30
30
31
31
- Function returning type `T`. An example declaration is
32
32
33
-
```
33
+
```cpp
34
34
T func( int i )
35
35
```
36
36
37
37
- Pointer to a function returning type `T`. An example declaration is
38
38
39
-
```
39
+
```cpp
40
40
T (*func)( int i )
41
41
```
42
42
43
43
- Reference to a function returning type `T`. An example declaration is
44
44
45
-
```
45
+
```cpp
46
46
T (&func)(int i)
47
47
```
48
48
49
49
- Pointer-to-member function dereference returning type `T`. Example function calls are
50
50
51
-
```
51
+
```cpp
52
52
(pObject->*pmf)();
53
53
(Object.*pmf)();
54
54
```
55
55
56
56
## Example
57
57
The following example calls the standard library function `strcat_s` with three arguments:
58
58
59
-
```
59
+
```cpp
60
60
// expre_Function_Call_Operator.cpp
61
61
// compile with: /EHsc
62
62
@@ -89,7 +89,7 @@ Welcome to C++
89
89
## Function call results
90
90
A function call evaluates to an r-value unless the function is declared as a reference type. Functions with reference return type evaluate to l-values, and can be used on the left side of an assignment statement as follows:
91
91
92
-
```
92
+
```cpp
93
93
// expre_Function_Call_Results.cpp
94
94
// compile with: /EHsc
95
95
#include<iostream>
@@ -123,7 +123,7 @@ int main()
123
123
124
124
Functions that return class types, pointers to class types, or references to class types can be used as the left operand to member-selection operators. Therefore, the following code is legal:
Copy file name to clipboardExpand all lines: docs/cpp/function-overloading.md
+16-16Lines changed: 16 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -34,7 +34,7 @@ You can overload both member functions and non-member functions. The following t
34
34
## Example
35
35
The following example illustrates how overloading can be used.
36
36
37
-
```
37
+
```cpp
38
38
// function_overloading.cpp
39
39
// compile with: /EHsc
40
40
#include<iostream>
@@ -135,17 +135,17 @@ int print(double dvalue, int prec)
135
135
136
136
Consider the following declarations (the functions are marked `Variant 1`, `Variant 2`, and `Variant 3`, for identification in the following discussion):
137
137
138
-
```
138
+
```cpp
139
139
Fraction &Add( Fraction &f, long l ); // Variant 1
140
140
Fraction &Add( long l, Fraction &f ); // Variant 2
The intersection of these two sets is Variant 1. An example of an ambiguous function call is:
162
162
163
-
```
163
+
```cpp
164
164
F1 = Add( 3, 6 );
165
165
```
166
166
@@ -186,7 +186,7 @@ F1 = Add( 3, 6 );
186
186
187
187
However, the function overloading mechanism can distinguish between references that are qualified by **const** and `volatile` and references to the base type. This makes code such as the following possible:
User-defined conversions are applied if no built-in promotion or conversion exists. These conversions are selected on the basis of the type of the argument being matched. Consider the following code:
298
298
299
-
```
299
+
```cpp
300
300
// argument_matching1.cpp
301
301
classUDC
302
302
{
@@ -324,7 +324,7 @@ int main()
324
324
325
325
During the process of matching arguments, standard conversions can be applied to both the argument and the result of a user-defined conversion. Therefore, the following code works:
326
326
327
-
```
327
+
```cpp
328
328
void LogToFile( long l );
329
329
...
330
330
UDC udc;
@@ -335,7 +335,7 @@ LogToFile( udc );
335
335
336
336
If any user-defined conversions are required to match an argument, the standard conversions are not used when evaluating the best match. This is true even if more than one candidate function requires a user-defined conversion; in such a case, the functions are considered equal. For example:
337
337
338
-
```
338
+
```cpp
339
339
// argument_matching2.cpp
340
340
// C2668 expected
341
341
classUDC1
@@ -381,7 +381,7 @@ int main()
381
381
382
382
The `.` member-selection operator works exactly the same way, except that an implicit `&` (address-of) operator is prefixed to the object name. The following example shows how this works:
383
383
384
-
```
384
+
```cpp
385
385
// Expression encountered in code
386
386
obj.name
387
387
@@ -447,7 +447,7 @@ int main()
447
447
448
448
-`typedef` declarations do not define new types; they introduce synonyms for existing types. They do not affect the overloading mechanism. Consider the following code:
449
449
450
-
```
450
+
```cpp
451
451
typedefchar * PSTR;
452
452
453
453
voidPrint( char *szToPrint );
@@ -460,14 +460,14 @@ int main()
460
460
461
461
- The types "array of " and "pointer to" are considered identical for the purposes of distinguishing between overloaded functions. This is true only for singly dimensioned arrays. Therefore, the following overloaded functions conflict and generate an error message:
462
462
463
-
```
463
+
```cpp
464
464
voidPrint( char *szToPrint );
465
465
void Print( char szToPrint[] );
466
466
```
467
467
468
468
For multiply dimensioned arrays, the second and all succeeding dimensions are considered part of the type. Therefore, they are used in distinguishing between overloaded functions:
469
469
470
-
```
470
+
```cpp
471
471
void Print( char szToPrint[] );
472
472
void Print( char szToPrint[][7] );
473
473
void Print( char szToPrint[][9][42] );
@@ -483,7 +483,7 @@ If the base class function is not declared as 'virtual', then the derived class
483
483
484
484
Block scope is strictly observed; therefore, a function declared in file scope is not in the same scope as a function declared locally. If a locally declared function has the same name as a function declared in file scope, the locally declared function hides the file-scoped function instead of causing overloading. For example:
485
485
486
-
```
486
+
```cpp
487
487
// declaration_matching1.cpp
488
488
// compile with: /EHsc
489
489
#include<iostream>
@@ -517,7 +517,7 @@ int main()
517
517
518
518
Note that the call to `Deposit` in `Account::Deposit` calls the private member function. This call is correct because `Account::Deposit` is a member function and therefore has access to the private members of the class.
Copy file name to clipboardExpand all lines: docs/cpp/general-rules-and-limitations.md
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,15 +19,15 @@ ms.workload: ["cplusplus"]
19
19
20
20
- If a single module in your program contains both **dllimport** and `dllexport` declarations for the same function or object, the `dllexport` attribute takes precedence over the **dllimport** attribute. However, a compiler warning is generated. For example:
21
21
22
-
```
22
+
```cpp
23
23
__declspec( dllimport ) int i;
24
24
__declspec( dllexport ) int i; // Warning; inconsistent;
25
25
// dllexport takes precedence.
26
26
```
27
27
28
28
- In C++, you can initialize a globally declared or static local data pointer or with the address of a data object declared with the **dllimport** attribute, which generates an error in C. In addition, you can initialize a static local function pointer with the address of a function declared with the **dllimport** attribute. In C, such an assignment sets the pointer to the address of the DLL import thunk (a code stub that transfers control to the function) rather than the address of the function. In C++, it sets the pointer to the address of the function. For example:
29
29
30
-
```
30
+
```cpp
31
31
__declspec( dllimport ) void func1( void );
32
32
__declspec( dllimport ) int i;
33
33
@@ -45,7 +45,7 @@ ms.workload: ["cplusplus"]
45
45
46
46
However, because a program that includes the `dllexport` attribute in the declaration of an object must provide the definition for that object somewhere in the program, you can initialize a global or local static function pointer with the address of a `dllexport` function. Similarly, you can initialize a global or local static data pointer with the address of a `dllexport` data object. For example, the following code does not generate errors in C or C++:
47
47
48
-
```
48
+
```cpp
49
49
__declspec( dllexport ) void func1( void );
50
50
__declspec( dllexport ) int i;
51
51
@@ -63,22 +63,22 @@ ms.workload: ["cplusplus"]
63
63
64
64
The compiler generates the same warning if the base class is a specialization of a class template. To work around this, mark the base-class with `dllexport`. The problem with a specialization of a class template is where to place the **__declspec(dllexport)**; you are not allowed to mark the class template. Instead, explicitly instantiate the class template and mark this explicit instantiation with `dllexport`. For example:
65
65
66
-
```
66
+
```cpp
67
67
template class__declspec(dllexport) B<int>;
68
68
class__declspec(dllexport) D : public B<int> {
69
69
// ...
70
70
```
71
71
72
72
This workaround fails if the template argument is the deriving class. For example:
73
73
74
-
```
74
+
```cpp
75
75
class __declspec(dllexport) D : public B<D> {
76
76
// ...
77
77
```
78
78
79
79
Because this is common pattern with templates, the compiler changed the semantics of `dllexport` when it is applied to a class that has one or more base-classes and when one or more of the base classes is a specialization of a class template. In this case, the compiler implicitly applies `dllexport` to the specializations of class templates. You can do the following and not get a warning:
Copy file name to clipboardExpand all lines: docs/cpp/general-rules-for-operator-overloading.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -55,7 +55,7 @@ The following rules constrain how overloaded operators are implemented. However,
55
55
56
56
Note that the meaning of any of the operators can be changed completely. That includes the meaning of the address-of (**&**), assignment (**=**), and function-call operators. Also, identities that can be relied upon for built-in types can be changed using operator overloading. For example, the following four statements are usually equivalent when completely evaluated:
Copy file name to clipboardExpand all lines: docs/cpp/how-catch-blocks-are-evaluated-cpp.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -36,7 +36,7 @@ C++ enables you to throw exceptions of any type, although in general it is recom
36
36
37
37
The order in which **catch** handlers appear is significant, because handlers for a given **try** block are examined in order of their appearance. For example, it is an error to place the handler for a base class before the handler for a derived class. After a matching **catch** handler is found, subsequent handlers are not examined. As a result, an ellipsis **catch** handler must be the last handler for its **try** block. For example:
0 commit comments