Class A
{
};
What is the difference between A a , A* a and A* a = new A().
A a;
Creates an instance of an A that lives on the stack using the default constructor.
A *a;
Is simply a uninitialized pointer to an A. It doesn't actually point to an A object at this point, but could. An initialized pointer (in this case, set to NULL) would look like so:
A *a = 0;
The difference here is that a null pointer does not point to any object while an uninitialized pointer might point anywhere. Initializing your pointers is a good practice to get into lest you find yourself wondering why your program is blowing up or yielding incorrect results.
Similarly, you don't want to attempt to dereference either a NULL pointer or an uninitialized pointer. But you can test the NULL pointer. Testing an uninitialized pointer yields undetermined and erroneous results. It may in fact be != 0 but certainly doesn't point anywhere you intend it to point. Make sure you initialize your pointers before testing them and test them before you attempt to dereference them.
A a = new A();
should be written as
A *a = new A();
and that creates a new A object that was allocated on the heap. The A object was created using the default constructor.
Where a default constructor is not explicitly written for a class, the compiler will implicitly create one though I don't believe the standard does not specify the state of data members for the object implicitly instantiated. For a discussion about implicit default constructors, see Martin York's response to this SO question.
A a calls the default constructor too, if you're going to mention that about the heap-allocated version.A *a; creates an uninitialized pointer. The only thing that you can do with it is assign a value to it (meaning, no comparisons or other uses). It might be worth adding a fourth example, A *a = 0, in which case a can be used in comparisons.A() is created with value-initialization. It doesn't use any contructors of A (in this case). Expression A() will use the default contructor only when it is explicitly declared by user. Otherwise, the initialization proceeds without using the constructor.struct A { ~A() { } int const a; };. Then new A() is perfectly valid (and initializes a to zero). But if it would use the default constructor like in A a; or new A it would be ill-formed (because of the const member). So it makes a big difference whether the default constructor is used or not.A a declares an instance of A named a
A *a declares a pointer to a A class
A *a = new A() allocates space for a on the heap and calls the proper constructor (if no constructor is specified, it performs default initialization).
For further information about the last form see http://en.wikipedia.org/wiki/New_%28C%2B%2B%29
A* a = ...a on the heap. It also initializes the A object, and declares a pointer to it.A. If it is declared in a function, it is put on the call stack.