int x;
I'm sure you understand that this defines an int
object named x
. Did you use new
? How about if we add copy-initialization:
int x = 5;
This defines an int
object named x
and initialises it to 5. The same result can be achieved with direct-initialization:
int x(5);
So what about:
data aaa(2011,7,1);
This is precisely like the above syntax. We're creating an object of type data
called aaa
, just this time we're passing three arguments to its constructor.
The thing that all of these definitions have in common is that they create an object of a certain type with automatic storage duration. This means that the objects will be destroyed at the end of their scope.
Now consider the following line:
int* p = new int(5);
This creates two objects. The first is an int*
called p
. That int*
has automatic storage duration, just as above, and will be destroyed at the end of its scope. We then initialise that object with the pointer returned by new int(5)
. This new-expression creates an int
object, initialized to 5, with dynamic storage duration. That means that it is not destroyed automatically at the end of its scope, but needs to be delete
d.
So, in short, these are two different ways to create objects that give them different storage durations.