0
whats is the difference between these three statements in c++ ??
aa *obj;   
aa *obj1 = new aa; 
aa *obj2 = new aa();

where aa is a class I am confucion in last two statement .

Gaurav
  • 1
  • 2
  • 1
    Two of them are explained here: http://stackoverflow.com/questions/620137/do-the-parentheses-after-the-type-name-make-a-difference-with-new – chris Apr 04 '15 at 16:02
  • He did say it was a class, not a built-in data type. I guess it could be a POD, but that's really old compilers that care. – JDługosz Apr 04 '15 at 16:04
  • @JDługosz What matters is the language specification, not the compiler's age. – juanchopanza Apr 04 '15 at 16:09
  • What do you mean? Years ago, it worked one way. Now, *conforming* compilers work another way. Spec or not, many of us had to cope with Visual Studio pre-7.1 when templates and such details did not work as specified. – JDługosz Apr 04 '15 at 16:27

1 Answers1

1

The first does not initialize the pointer.

In the latest specification,

  • If the new-initializer is omitted, the object is default-initialized (8.5); if no initialization is performed, the object has indeterminate value.
  • Otherwise, the new-initializer is interpreted according to the initialization rules of 8.5 for direct initialization.

That is, if the class (you said it was a class) doesn't have a constructor, then the first form will act the same as a local scope definition and leave the memory un-initialized.

The empty initializer will force it to be initialized anyway, which gives the same results as a global variable of that type.

A class might not have a constructor, even a hidden constructor, if it contains nothing but data members of primitive types. You'll see that discussed as a "POD", or plain'ol data. For templates, the difference was found to be annoying, so the rules were refined to work, with (), uniformly for any type, even built-in types. new int() will give a pointer to a value holding 0. new int will give a pointer to a value holding whatever garbage happened to be at that address before.

JDługosz
  • 5,592
  • 3
  • 24
  • 45
  • The second and third statements can differ in meaning depending on the definition of `aa`. – juanchopanza Apr 04 '15 at 16:08
  • I stand corrected. I knew it changed, but got that backwards. . – JDługosz Apr 04 '15 at 16:42
  • Can anyone clarify on this " the 1st statement [ aa *obj ] Will initialized pointer *obj automatically will address of class aa object . now the issue is - where's the object of aa is created and how its initialized implicit *obj pointer??? because i have just defined the pointer of class aa but not initialized it with aa address...???? – Gaurav Apr 04 '15 at 17:24
  • `obj` is *not initialized* in the first statement. No object is created; nothing happens. Don't write that: declare the variable when you are ready to give it a value. Write `aa* obj= nullptr;` if you must leave it without a meaningful pointer. Also, note that in C++, unlike C, the preferred style is to put the star with the type, not the variable. Try the classic: Stroustrup's *The C++ Programming Language*. – JDługosz Apr 05 '15 at 13:10
  • The Question does not contain a program; just 3 variable declarations. – JDługosz May 04 '15 at 17:13