1

Possible Duplicate:
Do the parentheses after the type name make a difference with new?

Whats the difference between the following initialisations? In the tutorial, it is as in case #1 but does it make any difference if i use the #2 way below?

struct X
{
    X() {}
    int x;
};

int main()
{
    std::auto_ptr<X> p1(new X);   // #1
    std::auto_ptr<X> p2(new X()); // #2
}
Community
  • 1
  • 1
user1086635
  • 1,536
  • 2
  • 15
  • 21
  • See this good reference: http://stackoverflow.com/questions/620137/do-the-parentheses-after-the-type-name-make-a-difference-with-new – JRL Feb 05 '12 at 02:36

1 Answers1

1

The smart pointer doesn't make any difference here. Both smart pointers are initialized in the same way, with a pointer to X. The difference is how X is initialized. If there is a difference and what the difference is depends on how X is defined. This answer has an excellent description of what happens in different cases. In this case since X has a default constructor they get initialized the same. However if there were no default constructor they would be initialized differently.

Community
  • 1
  • 1
David Brown
  • 13,336
  • 4
  • 38
  • 55