Sign in to follow this  
jochen

What is the difference between new X and new X()

Recommended Posts

jochen    333
In C++, Assumed X is a class type. What is the difference between the two versions of the calls?
// without brackets
new X;

// with brackets
new X();

Is there any? If not, does somebody know if there is a difference in managed C++ ? Best Jochen

Share this post


Link to post
Share on other sites
I would say there is no difference, but I'm not absolutely sure. Maybe someone can come up with some examples which ly in the obscure dusty corners (as my c++ teacher liked to name them [wink]) of c++ in which it isn't the same.

But thinking about it, I can't come up with any examples in which they aren't the same. Maybe you can create something with default arguments to an constructor, something like this:



class X
{
public:
// default constructor
X();

// other constructor expecting an int argument (with a default value defined)
X(int n = 0);
};





I'm not sure what happens in this case, I think this doesn't compile, because its unknown which constructor you mean, otherwise it might always choose the default constructor if you're constructing a class without any arguments.

Share this post


Link to post
Share on other sites
cignox1    735
AFAIK there are subtle differences between the two things, concerning initialization or wich contructor is called. As a guess (that most probably is at least imprecise) when you call new X the values are not initialized unless the default contructor is defined. That is, when you define a constructor with parameters the c++ compiler will remove the default contructor and you will need to explicitly define one. Perhaps (and I say perhaps) when you call new X and there aren' default contructor, the object is not initializated.

Share this post


Link to post
Share on other sites
Nitage    1107
For primitive types:
new X();//creates a new primitive type, default initialized
new X;//creates a new primitive type, unititialized

For class types:
new X();//creates a new class type, initialized with default constructor
new X;//creates a new class type, WITH ALL PRIMITIVE MEMBERS DEFAULT INITIALIZED then initialized with default constructor.


class foo
{
public:
int bar1;
int bar2;

foo()
:bar1(1)
{
}
};

foo* ptr = new foo;
ptr->bar1 == 1;
ptr->bar2 == ???;

foo* ptr = new foo();
ptr->bar1 == 1;
ptr->bar2 == 0;







HOWEVER - don't use this behaviour.

  • Many very widely used compilers don't implement the behaviour correctly (including the MS ones).

  • Even if this behaviour was supported, it's a one way ticket to obscure code

  • Do you really want the internal state of your class to depend on whether the creator used brackets to create it?

Share this post


Link to post
Share on other sites
SiCrane    11839
Quote:
Original post by Nitage
For class types:
new X();//creates a new class type, initialized with default constructor
new X;//creates a new class type, WITH ALL PRIMITIVE MEMBERS DEFAULT INITIALIZED then initialized with default constructor.

Incorrect.
new X:
* if X is non-POD, the object is default initialized
* if X is POD, then the object is uninitialized
new X():
* the object is default initialized

POD: essentially, either a primitive or a class that doesn't have a user defined copy constructor, assignment operator or destructor and has only primitive or POD members.

default initialized:
* if X is non-POD, the default constructor of X is called
* if X is an array, each element is default initialized
* otherwise X is zero-initialized

zero-initialized:
* if X is scalar (your basic primitive types) the storage is set to 0 converted to X (this may not be an all 0 bit pattern. Pointers to members often contain non-0 bit patterns as part of a zero-initialization)
* if X is a class type, every member is zero-initialized
* if X is a union, the first member is zero-initialized
* if X is an array, each element is zero-initialized

See section 5.3.4 paragraph 15, section 8.5 paragraph 5 and section 9 paragraph 4 in the C++ standard.

Share this post


Link to post
Share on other sites
cignox1    735
I think I wouln't ever be able to create something more involuted :-) And I still wonder why c++ compilers always miss some features (or have non-standard behaviours)...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this