Skip to main content
Left closed in review as "Original close reason(s) were not resolved" by Adrian Mole, Mark Rotteveel, Intsab Haider
edited tags
Link
R. Martinho Fernandes
  • 236k
  • 73
  • 443
  • 518
Post Closed as "Duplicate" by Lightness Races in Orbit c++
deleted 1 characters in body
Source Link
Mark Garcia
  • 17.9k
  • 4
  • 60
  • 95

I've been writing C and C++ code for almost twenty years, but there's one aspect of these languages that I've never really understood. I've obviously used regular casts (ii.e.

MyClass *m = (MyClass *)ptr; 

all over the place, but there seem to be two other types of casts, and I don't know the difference. What's the difference between the following lines of code?

MyClass *m = (MyClass *)ptr; MyClass *m = static_cast<MyClass *>(ptr); MyClass *m = dynamic_cast<MyClass *>(ptr); 

I've been writing C and C++ code for almost twenty years, but there's one aspect of these languages that I've never really understood. I've obviously used regular casts (i.e.

MyClass *m = (MyClass *)ptr; 

all over the place, but there seem to be two other types of casts, and I don't know the difference. What's the difference between the following lines of code?

MyClass *m = (MyClass *)ptr; MyClass *m = static_cast<MyClass *>(ptr); MyClass *m = dynamic_cast<MyClass *>(ptr); 

I've been writing C and C++ code for almost twenty years, but there's one aspect of these languages that I've never really understood. I've obviously used regular casts i.e.

MyClass *m = (MyClass *)ptr; 

all over the place, but there seem to be two other types of casts, and I don't know the difference. What's the difference between the following lines of code?

MyClass *m = (MyClass *)ptr; MyClass *m = static_cast<MyClass *>(ptr); MyClass *m = dynamic_cast<MyClass *>(ptr); 
edited tags
Link
John Dibling
  • 101.9k
  • 34
  • 192
  • 335
This question is irrelevant to C programming so I removed the C tag.
Link
Loading
Source Link
Graeme Perrow
  • 57.6k
  • 24
  • 87
  • 126
Loading