Johan said:
Hi,
Suppose we have a class A.Why or when do you use
main()
{
A *ptr = new A;
// instead of
A ptr;
}
Johan
Use "A ptr;" whenever possible. Allocating A dynamically (with "new")
can be very slow, and usually means you must be careful to free the
memory (with "delete") later in the program.
The need to allocate an individual object dynamically is rare. If you
are allocating an arbitrarily sized collection of A, it is usually
better to rely on a container from the standard library to allocate and
hold your collection, than it is to call "new" yourself.
Btw, if A is not a type of pointer, you may want to rethink the name of
the variable "ptr."
-Jeff