Contents Up << >>
Why should I use "new" instead of trustworthy old malloc()?
Constructors/destructors, type safety, overridability.
- Constructors/destructors: unlike "malloc(sizeof(Fred))",
the call "new Fred()" calls
Fred's constructor. Similarly, "delete p" calls "*p"'s
destructor.
- Type safety: malloc() returns a "void*" which isn't
type safe. "new Fred()"
returns a ptr of the right type (a "Fred*").
- Overridability: "new" is an operator that can be overridden
by a class, while "malloc" is not overridable on a per-class
basis.