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
Elsewhere, a user can declare an instance of the template that is specialized for int. Assume that get_a() and get_b() are functions that return an int:
32
32
33
-
```
33
+
```cpp
34
34
int a = get_a();
35
35
int b = get_b();
36
36
int i = minimum<int>(a, b);
@@ -44,7 +44,7 @@ int i = minimum(a, b);
44
44
45
45
When the compiler encounters that last statement, it generates a new function in which every occurrence of *T* in the template is replaced with `int`:
46
46
47
-
```
47
+
```cpp
48
48
49
49
intminimum(const int& lhs, const int& rhs)
50
50
{
@@ -66,7 +66,7 @@ template <typename T, typename U, typename V> class Foo{};
66
66
67
67
The keyword `class` is equivalent to `typename` in this context. You can express the previous example as:
68
68
69
-
```
69
+
```cpp
70
70
template <classT, class U, class V> class Foo{};
71
71
```
72
72
@@ -103,7 +103,7 @@ int main()
103
103
104
104
There is no inherent requirement that the type arguments for any particular template all belong to the same object hierarchy, although you can define a template that enforces such a restriction. You can combine object-oriented techniques with templates; for example, you can store a Derived* in a vector\<Base\*>. Note that the arguments must be pointers
105
105
106
-
```
106
+
```cpp
107
107
vector<MyClass*> vec;
108
108
MyDerived d(3, L"back again", time(0));
109
109
vec.push_back(&d);
@@ -118,7 +118,7 @@ vector<MyClass*> vec;
118
118
## Non-type parameters
119
119
Unlike generic types in other languages such as C# and Java, C++ templates support non-type parameters, also called value parameters. For example, you can provide a constant integral value to specify the length of an array, as with this example that is similar to the std::array class in the Standard Library:
Copy file name to clipboardExpand all lines: docs/cpp/temporary-objects.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
@@ -18,7 +18,7 @@ In some cases, it is necessary for the compiler to create temporary objects. The
18
18
19
19
- To store the return value of a function that returns a user-defined type. These temporaries are created only if your program does not copy the return value to an object. For example:
20
20
21
-
```
21
+
```cpp
22
22
UDT Func1(); // Declare a function that returns a user-defined
Copy file name to clipboardExpand all lines: docs/cpp/this-pointer.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,26 +19,26 @@ The **this** pointer is a pointer accessible only within the nonstatic member fu
19
19
20
20
```
21
21
22
-
this
22
+
this
23
23
this->member-identifier
24
24
```
25
25
26
26
## Remarks
27
27
An object's **this** pointer is not part of the object itself; it is not reflected in the result of a `sizeof` statement on the object. Instead, when a nonstatic member function is called for an object, the address of the object is passed by the compiler as a hidden argument to the function. For example, the following function call:
28
28
29
-
```
29
+
```cpp
30
30
myDate.setMonth( 3 );
31
31
```
32
32
33
33
can be interpreted this way:
34
34
35
-
```
35
+
```cpp
36
36
setMonth( &myDate, 3 );
37
37
```
38
38
39
39
The object's address is available from within the member function as the **this** pointer. Most uses of **this** are implicit. It is legal, though unnecessary, to explicitly use **this** when referring to members of the class. For example:
40
40
41
-
```
41
+
```cpp
42
42
void Date::setMonth( int mn )
43
43
{
44
44
month = mn; // These three statements
@@ -49,13 +49,13 @@ void Date::setMonth( int mn )
49
49
50
50
The expression `*this` is commonly used to return the current object from a member function:
51
51
52
-
```
52
+
```cpp
53
53
return *this;
54
54
```
55
55
56
56
The **this** pointer is also used to guard against self-reference:
57
57
58
-
```
58
+
```cpp
59
59
if (&Object != this) {
60
60
// do not execute in cases of self-reference
61
61
```
@@ -67,7 +67,7 @@ if (&Object != this) {
67
67
68
68
## Example
69
69
70
-
```
70
+
```cpp
71
71
// this_pointer.cpp
72
72
// compile with: /EHsc
73
73
@@ -140,7 +140,7 @@ your buffer
140
140
141
141
Consider this example:
142
142
143
-
```
143
+
```cpp
144
144
// type_of_this_pointer1.cpp
145
145
classPoint
146
146
{
@@ -153,7 +153,7 @@ int main()
153
153
154
154
The preceding code declares a member function, `X`, in which the **this** pointer is treated as a **const** pointer to a **const** object. Combinations of *cv-mod-list* options can be used, but they always modify the object pointed to by **this**, not the **this** pointer itself. Therefore, the following declaration declares function `X`; the **this** pointer is a **const** pointer to a **const** object:
Copy file name to clipboardExpand all lines: docs/cpp/tokens-cpp.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,13 +32,13 @@ A token is the smallest element of a C++ program that is meaningful to the compi
32
32
33
33
The parser separates tokens from the input stream by creating the longest token possible using the input characters in a left-to-right scan. Consider this code fragment:
34
34
35
-
```
35
+
```cpp
36
36
a = i+++j;
37
37
```
38
38
39
39
The programmer who wrote the code might have intended either of these two statements:
Copy file name to clipboardExpand all lines: docs/cpp/transfers-of-control.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
@@ -16,7 +16,7 @@ You can use the `goto` statement or a **case** label in a `switch` statement to
16
16
17
17
The following example shows a loop that declares and initializes the objects `total`, `ch`, and `i`. There is also an erroneous `goto` statement that transfers control past an initializer.
18
18
19
-
```
19
+
```cpp
20
20
// transfers_of_control.cpp
21
21
// compile with: /W1
22
22
// Read input until a nonnumeric character is entered.
Copy file name to clipboardExpand all lines: docs/cpp/try-throw-and-catch-statements-cpp.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,7 +25,7 @@ To implement exception handling in C++, you use `try`, `throw`, and `catch` expr
25
25
26
26
## Example
27
27
28
-
```
28
+
```cpp
29
29
30
30
MyData md;
31
31
try {
@@ -63,7 +63,7 @@ MyData GetNetworkResource()
63
63
64
64
A `throw` expression that has no operand re-throws the exception currently being handled. We recommend this form when re-throwing the exception, because this preserves the original exception’s polymorphic type information. Such an expression should only be used in a `catch` handler or in a function that's called from a `catch` handler. The re-thrown exception object is the original exception object, not a copy.
Copy file name to clipboardExpand all lines: docs/cpp/typeid-operator.md
+5-7Lines changed: 5 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,10 +16,8 @@ ms.workload: ["cplusplus"]
16
16
17
17
```
18
18
19
-
typeid(
20
-
type-id
21
-
)
22
-
typeid( expression )
19
+
typeid(type-id)
20
+
typeid(expression)
23
21
```
24
22
25
23
## Remarks
@@ -39,7 +37,7 @@ typeid( expression )
39
37
40
38
If the *expression* points to a base class type, yet the object is actually of a type derived from that base class, a **type_info** reference for the derived class is the result. The *expression* must point to a polymorphic type (a class with virtual functions). Otherwise, the result is the **type_info** for the static class referred to in the *expression*. Further, the pointer must be dereferenced so that the object it points to is used. Without dereferencing the pointer, the result will be the **type_info** for the pointer, not what it points to. For example:
41
39
42
-
```
40
+
```cpp
43
41
// expre_typeid_Operator.cpp
44
42
// compile with: /GR /EHsc
45
43
#include<iostream>
@@ -68,7 +66,7 @@ int main() {
68
66
69
67
If the *expression* is neither a pointer nor a reference to a base class of the object, the result is a **type_info** reference representing the static type of the *expression*. The *static type* of an expression refers to the type of an expression as it is known at compile time. Execution semantics are ignored when evaluating the static type of an expression. Furthermore, references are ignored when possible when determining the static type of an expression:
70
68
71
-
```
69
+
```cpp
72
70
// expre_typeid_Operator_2.cpp
73
71
#include <typeinfo>
74
72
@@ -80,7 +78,7 @@ int main()
80
78
81
79
**typeid** can also be used in templates to determine the type of a template parameter:
0 commit comments