R
Razvan
Hi !
Is there any difference between new X and new X() ?!
Regards,
Razvan
Is there any difference between new X and new X() ?!
Regards,
Razvan
Razvan said:Is there any difference between new X and new X() ?!
Is there any difference between new X and new X() ?!
There used to be, but only if X is a POD. The first form would
leave it uninitialised, the second would default-initialise it.
Nowadays I am not sure.
Rob said:Victor Bazarov wrote in in comp.lang.c++:
Nowadays its value-initialized, which is a kind-of recursive
default-intialization.
In 8.5/5
To value-initialize an object of type T means:
— if T is a class type (clause 9) with a user-declared
constructor (12.1), then the default constructor for T
is called (and the initialization is ill-formed if T
has no accessible default constructor);
— if T is a non-union class type without a user-declared
constructor, then every non-static data member and
base-class component of T is value-initialized;
8.5/7
An object whose initializer is an empty set of parentheses,
i.e., (), shall be value-initialized.
Thanks, Rob. So, in the case of 'new X', if X is a POD it is still
_uninitialised_. The only difference now is that it's not "default-
initialised" for PODs but "value-initialised", right?
You don't have to reply, I'm just repeating here what Andrew Koenig's
reply stated (I hope).
Andrew Koenig said:Yes, but it is very rare that you should care.
If you say "new X", the initial value of the object allocated is the same as
an uninitialized local variable of the same type would have. So, for
example, the result of "new int" is the address of an uninitialized int, and
the result of "new std::string" is the address of a std::string object with
no characters (because all strings are initialized, if only by the default
constructor). In contrast, the result of "new int()" is the address of an
int with value 0, and the result of "new std::string()" is exactly the same
as the result of "new std::string" (because all strings are initialized, if
only by the default constructor).
This is a relatively recent feature, so if you write a program that relies
on it, you may wish to make sure that your compiler implements it correctly.
Razvan said:"Andrew Koenig" <[email protected]> wrote in message
This is valid only for the above mentioned POD type. For a
class the call new MyClass is the same with the call new MyClass()
(assuming the class has a default constructor). At least this is what
I understood from you and the previoud posters.
I am rewritting your expression: when calling new X (X is a POD
type) a new object X is created that it is not innitialized.
I see. For a POD (like an int) the call new int return a
pointer to an int that is not innitialized (it is not guaranteed to be
zero). OTOH new int() guarantees that the pointer points to an int
innitialized with zero.
Correct.
For a string it does not matter how you create it because it
is a class that has a constructor and the constructor is called
anyway.
Bottom line:
int* pInt = new int; // the pointer points to an unnitialized int
int* pInt2 = new int() // the pointer points to an int that is
guaranteed to be innitialized to 0
MyClass tst = new MyClass;
is identical with:
MyClass tst2 = new MyClass();
That mean you should better use new X
Andrew said:Why?
For being consistant , look:
string s1
string s2 = new string
string s1();//<- won't work
string *s2 = new string()
object of type Foo that has the same value that an otherwise uninitialized
local variable of type Foo has, namely whatever value is obtained by running
the Foo constructor.
So I stand by my original statement.
Only if you have explicitly defined a constructor for MyClass.
I do not understand. After running the constructor the object
is innitialized.
How about the default constructor ?
Perhaps I am missing something here. Let's suppose I have the
class:
class CTest
{
int counter;
CTest(){}
}
The constructor for CTest does not innitialize the variable
counter. If I create an object:
CTest test;
is the attribute counter guaranteed to be 0 ? (I mean the constructor
does nothing about it; what is happening is this case?)
Andrew said:The important case is this one:
class STest {
int counter;
string xyzzy;
};
STest test;
Here, you did not define a constructor for STest, but it effectively has one
anyway, by virtue of having a data member with a constructor. Accordingly,
if "test" is a local variable, the value of test.counter will be undefined.
On the other hand, if you write
STest *p = new STest();
then p will point to an STest object with its counter member initialized to
zero.
Karl said:Sure about that?
As I see it: STest is not a POD (due to string member xyzzy), thus
the zero initialization does not happen.
If STest would look like this
class STest {
int counter;
char* xyzzy;
};
then
STest *p = new STest();
would initialize counter and xyzzy to 0
( I really hate this rule. If it would be dropped from C++ I wouldn't
leave a single tear for it. Same for implicite return 0 in main()
Let's analyse... Unless something is different in the new edition (which
I don't have), here we go:
Sure about that?
As I see it: STest is not a POD (due to string member xyzzy), thus
the zero initialization does not happen.
Andrew said:Yes, I'm sure.
It's different in the 2003 standard.
See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html, issue 178.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.