[Cs3] Questions

Mikhail Nesterenko mikhail at cs.kent.edu
Wed Mar 23 16:24:16 EDT 2022


> 
> I'm still a little confused on some of the topics from today's lecture. Can
> you please answer the following questions?
> 
> 1. What is the difference between static_cast, reinterpret_cast,  and
> dynamic _ cast. Also is dynamic only for pointers?


dynamic_cast converts glvalue or lvalue (as described earlier) to
pointer or reference. More details here:

   https://en.cppreference.com/w/cpp/language/dynamic_cast

static_cast: a lot more cases. Basically, the only reason to use
dynamic_cast is to ascertain that the object the pointer points to is
indeed of the requested type. This requires extra lookup but is
"safer" since if the object is not of the requested type dynamic_cast
returns nullptr and throws an exception.


> 2. When would you use one type of casting rather than the other?

see above

> 3.  Are Abstract Factory and Factory two different design patterns? If so,
> what is the difference?

Factory is an object creating other objects. It is not a pattern in
the sense of Gamma et al, just a term.

Factory Method is considered a pattern, and it is defined as such in
the Gamma book. It has subclasses that implement object creation. More
details here:

    https://en.wikipedia.org/wiki/Factory_method_pattern

The Abstract Factory uses this Factory Method Pattern to do this: have
a collection of factories that create similar products.

I do not cover Factory Method separately and include it in the
Abstract Factory Design Pattern coverage.

> 
> 4. What are some specific use cases for the different types of casting,
> which one is preferred?

see answer to point 1.


More information about the cs3 mailing list