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
{{ message }}
Repository navigation
Commit 1b0f246
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: docs/windows/override-cpp-component-extensions.md
+17-18Lines changed: 17 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,21 +12,20 @@ ms.author: "mblome"
12
12
ms.workload: ["cplusplus", "uwp"]
13
13
---
14
14
# override (C++ Component Extensions)
15
-
The `override` context-sensitive keyword indicates that a member of a type overrides a base class or a base interface member.
15
+
The **override** context-sensitive keyword indicates that a member of a type overrides a base class or a base interface member.
16
16
17
17
## Remarks
18
-
The `override` keyword is valid when compiling for native targets (default compiler option), Windows Runtime targets (**/ZW** compiler option), or common language runtime targets (**/clr** compiler option).
18
+
The **override** keyword is valid when compiling for native targets (default compiler option), Windows Runtime targets (`/ZW` compiler option), or common language runtime targets (`/clr` compiler option).
19
19
20
20
For more information about override specifiers, see [override Specifier](../cpp/override-specifier.md) and [Override Specifiers and Native Compilations](../dotnet/how-to-declare-override-specifiers-in-native-compilations-cpp-cli.md).
21
21
22
22
For more information about context-sensitive keywords, see [Context-Sensitive Keywords](../windows/context-sensitive-keywords-cpp-component-extensions.md).
23
23
24
24
## Examples
25
-
**Example**
26
25
27
-
The following code example shows that `override` can also be used in native compilations.
26
+
The following code example shows that **override** can also be used in native compilations.
28
27
29
-
```cpp#
28
+
```cpp
30
29
// override_keyword_1.cpp
31
30
// compile with: /c
32
31
structI1 {
@@ -38,11 +37,11 @@ struct X : public I1 {
38
37
};
39
38
```
40
39
41
-
**Example**
40
+
### Example
41
+
42
+
The following code example shows that **override** can be used in Windows Runtime compilations.
42
43
43
-
The following code example shows that `override` can be used in Windows Runtime compilations.
44
-
45
-
```cpp#
44
+
```cpp
46
45
// override_keyword_2.cpp
47
46
// compile with: /ZW /c
48
47
ref struct I1 {
@@ -54,15 +53,15 @@ ref struct X : public I1 {
54
53
};
55
54
```
56
55
57
-
**Requirements**
58
-
59
-
Compiler option: **/ZW**
60
-
61
-
**Example**
56
+
#### Requirements
62
57
63
-
The following code example shows that `override` can be used in common language runtime compilations.
58
+
Compiler option: `/ZW`
59
+
60
+
### Example
61
+
62
+
The following code example shows that **override** can be used in common language runtime compilations.
Copy file name to clipboardExpand all lines: docs/windows/overview-of-generics-in-visual-cpp.md
+34-40Lines changed: 34 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,75 +18,69 @@ Generics are parameterized types supported by the common language runtime. A par
18
18
C++ supports templates and both templates and generics support parameterized types to create typed collection classes. However, templates provide compile-time parameterization. You cannot reference an assembly containing a template definition and create new specializations of the template. Once compiled, a specialized template looks like any other class or method. In contrast, generics are emitted in MSIL as a parameterized type known by the runtime to be a parameterized type; source code that references an assembly containing a generic type can create specializations of the generic type. For more information on the comparison of Visual C++ templates and generics, see [Generics and Templates (Visual C++)](../windows/generics-and-templates-visual-cpp.md).
19
19
20
20
## Generic Functions and Types
21
-
Class types, as long as they are managed types, may be generic. An example of this might be a `List` class. The type of an object in the list would be the type parameter. If you needed a `List` class for many different types of objects, before generics you might have used a `List` that takes **System::Object** as the item type. But that would allow any object (including objects of the wrong type) to be used in the list. Such a list would be called an untyped collection class. At best, you could check the type at runtime and throw an exception. Or, you might have used a template, which would lose its generic quality once compiled into an assembly. Consumers of your assembly could not create their own specializations of the template. Generics allow you to create typed collection classes, say `List<int>` (read as "List of int") and `List<double>` ("List of double") which would generate a compile-time error if you tried to put a type that the collection was not designed to accept into the typed collection. In addition, these types remain generic after they are compiled.
21
+
Class types, as long as they are managed types, may be generic. An example of this might be a `List` class. The type of an object in the list would be the type parameter. If you needed a `List` class for many different types of objects, before generics you might have used a `List` that takes `System::Object` as the item type. But that would allow any object (including objects of the wrong type) to be used in the list. Such a list would be called an untyped collection class. At best, you could check the type at runtime and throw an exception. Or, you might have used a template, which would lose its generic quality once compiled into an assembly. Consumers of your assembly could not create their own specializations of the template. Generics allow you to create typed collection classes, say `List<int>` (read as "List of int") and `List<double>` ("List of double") which would generate a compile-time error if you tried to put a type that the collection was not designed to accept into the typed collection. In addition, these types remain generic after they are compiled.
22
22
23
-
A description of the syntax of generic classes may be found in [Generic Classes (C++/CLI)](../windows/generic-classes-cpp-cli.md)`.` A new namespace, <xref:System.Collections.Generic>, introduces a set of parameterized collection types including <xref:System.Collections.Generic.Dictionary%602>, <xref:System.Collections.Generic.List%601> and <xref:System.Collections.Generic.LinkedList%601>.
23
+
A description of the syntax of generic classes may be found in [Generic Classes (C++/CLI)](../windows/generic-classes-cpp-cli.md). A new namespace, <xref:System.Collections.Generic>, introduces a set of parameterized collection types including <xref:System.Collections.Generic.Dictionary%602>, <xref:System.Collections.Generic.List%601> and <xref:System.Collections.Generic.LinkedList%601>.
24
24
25
-
Both instance and static class member functions, delegates, and global functions may also be generic. Generic functions may be necessary if the function's parameters are of an unknown type, or if the function itself must work with generic types. In many cases where **System::Object** may have been used in the past as a parameter for an unknown object type, a generic type parameter may be used instead, allowing for more type-safe code. Any attempt to pass in a type that the function was not designed for would be flagged as an error at compile time. Using **System::Object** as a function parameter, the inadvertent passing of an object that the function wasn't intended to deal with would not be detected, and you would have to cast the unknown object type to a specific type in the function body, and account for the possibility of an InvalidCastException. With a generic, code attempting to pass an object to the function would cause a type conflict so the function body is guaranteed to have the correct type.
25
+
Both instance and static class member functions, delegates, and global functions may also be generic. Generic functions may be necessary if the function's parameters are of an unknown type, or if the function itself must work with generic types. In many cases where `System::Object` may have been used in the past as a parameter for an unknown object type, a generic type parameter may be used instead, allowing for more type-safe code. Any attempt to pass in a type that the function was not designed for would be flagged as an error at compile time. Using `System::Object` as a function parameter, the inadvertent passing of an object that the function wasn't intended to deal with would not be detected, and you would have to cast the unknown object type to a specific type in the function body, and account for the possibility of an InvalidCastException. With a generic, code attempting to pass an object to the function would cause a type conflict so the function body is guaranteed to have the correct type.
26
26
27
-
The same benefits apply to collection classes built on generics. Collection classes in the past would use **System::Object** to store elements in a collection. Insertion of objects of a type that the collection was not designed for was not flagged at compile time, and often not even when the objects were inserted. Usually, an object would be cast to some other type when it was accessed in the collection. Only when the cast failed would the unexpected type be detected. Generics solves this problem at compile time by detecting any code that inserts a type that doesn't match (or implicitly convert to) the type parameter of the generic collection.
27
+
The same benefits apply to collection classes built on generics. Collection classes in the past would use `System::Object` to store elements in a collection. Insertion of objects of a type that the collection was not designed for was not flagged at compile time, and often not even when the objects were inserted. Usually, an object would be cast to some other type when it was accessed in the collection. Only when the cast failed would the unexpected type be detected. Generics solves this problem at compile time by detecting any code that inserts a type that doesn't match (or implicitly convert to) the type parameter of the generic collection.
28
28
29
29
For a description of the syntax, see [Generic Functions (C++/CLI)](../windows/generic-functions-cpp-cli.md).
30
30
31
31
## Terminology Used With Generics
32
32
33
-
#####Type Parameters
34
-
A generic declaration contains one or more unknown types known as *type parameters*. Type parameters are given a name which stands for the type within the body of the generic declaration. The type parameter is used as a type within the body of the generic declaration. The generic declaration for List<T\> contains the type parameter T.
33
+
### Type Parameters
34
+
A generic declaration contains one or more unknown types known as *type parameters*. Type parameters are given a name which stands for the type within the body of the generic declaration. The type parameter is used as a type within the body of the generic declaration. The generic declaration for `List<T>` contains the type parameter T.
35
35
36
-
#####Type Arguments
37
-
The *type argument* is the actual type used in place of the type parameter when the generic is specialized for a specific type or types. For example, `int` is the type argument in `List<int>`. Value types and handle types are the only types allowed in as a generic type argument.
36
+
### Type Arguments
37
+
The *type argument* is the actual type used in place of the type parameter when the generic is specialized for a specific type or types. For example, **int** is the type argument in `List<int>`. Value types and handle types are the only types allowed in as a generic type argument.
38
38
39
-
#####Constructed Type
39
+
### Constructed Type
40
40
A type constructed from a generic type is referred to as a *constructed type*. A type not fully specified, such as `List<T>` is an *open constructed type*; a type fully specified, such as `List<double>,` is a *closed constructed type* or *specialized type*. Open constructed types may be used in the definition of other generic types or methods and may not be fully specified until the enclosing generic is itself specified. For example, the following is a use of an open constructed type as a base class for a generic:
41
41
42
-
`// generics_overview.cpp`
43
-
44
-
`// compile with: /clr /c`
45
-
46
-
`generic <typename T>`
42
+
```cpp
43
+
// generics_overview.cpp
44
+
// compile with: /clr /c
45
+
generic <typename T>
47
46
48
-
`ref class List {};`
47
+
ref classList {};
49
48
50
-
`generic <typename T>`
49
+
generic <typenameT>
51
50
52
-
`ref class Queue : public List<T> {};`
51
+
ref class Queue : public List<T> {};
52
+
```
53
53
54
-
#####Constraint
54
+
### Constraint
55
55
A constraint is a restriction on the types that may be used as a type parameter. For example, a given generic class could accept only classes that inherit from a specified class, or implement a specified interface. For more information, see [Constraints on Generic Type Parameters (C++/CLI)](../windows/constraints-on-generic-type-parameters-cpp-cli.md).
56
56
57
57
## Reference Types and Value Types
58
-
Handles types and value types may be used as type arguments. In the generic definition, in which either type may be used, the syntax is that of reference types. For example, the **->** operator is used to access members of the type of the type parameter whether or not the type eventually used is a reference type or a value type. When a value type is used as the type argument, the runtime generates code that uses the value types directly without boxing the value types.
58
+
Handles types and value types may be used as type arguments. In the generic definition, in which either type may be used, the syntax is that of reference types. For example, the `->` operator is used to access members of the type of the type parameter whether or not the type eventually used is a reference type or a value type. When a value type is used as the type argument, the runtime generates code that uses the value types directly without boxing the value types.
59
59
60
60
When using a reference type as a generic type argument, use the handle syntax. When using a value type as a generic type argument, use the name of the type directly.
61
61
62
-
`// generics_overview_2.cpp`
63
-
64
-
`// compile with: /clr`
65
-
66
-
`generic <typename T>`
67
-
68
-
`ref class GenericType {};`
69
-
70
-
`ref class ReferenceType {};`
71
-
72
-
`value struct ValueType {};`
73
-
74
-
`int main() {`
62
+
```cpp
63
+
// generics_overview_2.cpp
64
+
// compile with: /clr
65
+
generic <typename T>
75
66
76
-
`GenericType<ReferenceType^> x;`
67
+
ref class GenericType {};
68
+
ref class ReferenceType {};
77
69
78
-
`GenericType<ValueType> y;`
70
+
value struct ValueType {};
79
71
80
-
`}`
72
+
int main() {
73
+
GenericType<ReferenceType^> x;
74
+
GenericType<ValueType> y;
75
+
}
76
+
```
81
77
82
78
## Type Parameters
83
79
Type parameters in a generic class are treated like other identifiers. However, because the type is not known, there are restrictions on their use. For example, you cannot use members and methods of the type parameter class unless the type parameter is known to support these members. That is, to access a member through the type parameter, you must add the type that contains the member to the type parameter's constraint list.
0 commit comments