Skip to content

Commit 5f5b299

Browse files
authored
Add example in C4541 warning reference
1 parent f6f9a03 commit 5f5b299

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

docs/error-messages/compiler-warnings/compiler-warning-level-1-c4541.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,32 @@ ms.assetid: b57b8f3e-117d-4fc2-bba6-faec17e5fa9d
1313
## Remarks
1414

1515
You tried to use the [`dynamic_cast` operator](../../cpp/dynamic-cast-operator.md) or [`typeid` operator](../../cpp/typeid-operator.md), which requires [Run-Time Type Information](../../cpp/run-time-type-information.md) (RTTI), without enabling it. To enable RTTI, recompile with [`/GR`](../../build/reference/gr-enable-run-time-type-information.md).
16+
17+
## Example
18+
19+
The following example generates C4541:
20+
21+
```cpp
22+
// C4541.cpp
23+
// compile with: /W1 /GR-
24+
25+
#include <typeinfo>
26+
27+
struct Base
28+
{
29+
virtual ~Base() {}
30+
};
31+
32+
struct Derived : Base {};
33+
34+
int main()
35+
{
36+
Derived derived;
37+
Base* pointer_to_base = &derived;
38+
39+
dynamic_cast<Derived*>(pointer_to_base); // C4541
40+
41+
typeid(*pointer_to_base); // C4541
42+
typeid(pointer_to_base); // OK
43+
}
44+
```

0 commit comments

Comments
 (0)