Why should I use a pointer rather than the object itself?

I’m coming from a Java background and have started working with objects in C++. But one thing that occurred to me is that people often use pointers to objects rather than the objects themselves, for example this declaration: Object *myObject = new Object; rather than: Object myObject; Or instead of using a function, let’s say … Read more

Regular cast vs. static_cast vs. dynamic_cast [duplicate]

This question already has answers here: When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used? (9 answers) Closed 7 years ago. 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; … Read more

When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?

What are the proper uses of: static_cast dynamic_cast const_cast reinterpret_cast C-style cast (type)value Function-style cast type(value) How does one decide which to use in which specific cases? 9 static_cast is the first cast you should attempt to use. It does things like implicit conversions between types (such as int to float, or pointer to void*), … Read more