C++ static cast or dynamic cast

May I know why's the answer static cast? And why isn't it dynamic cast? Do not understand. Thanks
1
2
3
4
5
6
7
8
9
10
11
12
Given following c code:
C *p = (C*)(
  malloc(sizeof(C)));
What is equivalent code in c++?   
C *p = interpret_cast<C*>(
  malloc(sizeof(C)));
    
C *p = dynamic_cast<C*>(
  malloc(sizeof(C)));
    
C *p = static_cast<C*>(
  malloc(sizeof(C)));
static_cast is compile time cast
dynamic_cast is runtime cast

you use dynamic_cast when type needs to be determined during runtime.

also there is no such thing as interpret_cast it's reinterpret_cast which interprets bytes from one type to another.
> Given following c code: C *p = malloc(sizeof(C)) ;
> What is equivalent code in c++?

Typically: auto p = std::make_unique<C>() ;
Last edited on
Topic archived. No new replies allowed.