-
July 26th, 2013, 06:36 PM
#1
Is there any difference between these two statements?
Here is the code,
Code:
class A
{
public:
A()
{
cout<<"A::A()"<<endl;
}
};
int main()
{
A* pA = new A();
A* pA2 = new A;
return 0;
}
Is there any difference between pA and pA2? Thanks.
-
July 26th, 2013, 10:05 PM
#2
Re: Is there any difference between these two statements?
Technically, no. Both will default-construct the A instance. I, personally, avoid using the empty pair of parentheses in this context, though, since this habit reduces the risk of mistakes like this one:
Guess what that really means?
Admittedly, that's a matter of personal taste to a large extent, though...
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
July 27th, 2013, 01:12 AM
#3
Re: Is there any difference between these two statements?
Originally Posted by LarryChen
Is there any difference between pA and pA2?
I note that this question is different from "Is there any difference between these two statements?" I shall assume that you are asking if there is a difference between new A() and new A.
The answer is yes: in the former, the object created is value initialised; in the latter, the object created is default initialised.
The thing is, value initialisation here means calling the default constructor since A is a class type with a user provided constructor. Likewise, default initialisation here means calling the default constructor since A is a class type. Hence, effectively, there is no difference, but there could be if say, A was an alias for a built-in integer type.
-
July 27th, 2013, 01:26 AM
#4
Re: Is there any difference between these two statements?
Originally Posted by LarryChen
Is there any difference between pA and pA2? Thanks.
There would be a difference if A were a POD (didn't have the constructor). Then the () case would be zero-initialized and the other would not. Here for example,
int* p = new int();
the allocated integer would be set to 0 but without the () it would not for certain.
-
July 27th, 2013, 01:48 AM
#5
Re: Is there any difference between these two statements?
Originally Posted by razzle
There would be a difference if A were a POD (didn't have the constructor).
Yes, though even if A were non-POD due to say, having a virtual function, if it did not have the constructor, there would still be a difference since value initialisation would mean zero initialisation rather than (only) the calling of the default constructor. Then again, since A has no member variables, it wouldn't really matter anyway.
-
July 27th, 2013, 05:04 AM
#6
Re: Is there any difference between these two statements?
Originally Posted by laserlight
, it wouldn't really matter anyway.
Well, if nothing else it makes a decent Zen koan to ponder intensively:
How much of nothing is there in an empty POD?
Last edited by razzle; July 27th, 2013 at 05:13 AM.
-
July 29th, 2013, 07:36 AM
#7
Re: Is there any difference between these two statements?
Originally Posted by laserlight
Yes, though even if A were non-POD due to say, having a virtual function
Every POD is a trivial type
not every trivial type is a POD
note that for C++11 the term 'POD' has mostly been replaced by 'trivial type' which is overall a good thing.
while your virtual function does make it a "non trivial type", it doesn't have an effect on the point you're making.
The only thing that matters is if the class has a default constructor or not.
If yes, then both new A; and new A(); call the default constructor
If no, then new A; constructs but doesn't necessarily initialize and new A() default-initializes.
-
July 29th, 2013, 08:59 AM
#8
Re: Is there any difference between these two statements?
Originally Posted by OReubens
while your virtual function does make it a "non trivial type", it doesn't have an effect on the point you're making.
The point that I'm making is that whether or not the class is a POD does not change the fact that "if it did not have the constructor, there would still be a difference since value initialisation would mean zero initialisation rather than (only) the calling of the default constructor". The mention of a virtual function was just an example to make the class a non-POD. I could have talked about a non-trivial class instead, but razzle mentioned POD, and mentioning a non-trivial class would not have an effect on the point that I was making.
Originally Posted by OReubens
The only thing that matters is if the class has a default constructor or not.
If yes, then both new A; and new A(); call the default constructor
If no, then new A; constructs but doesn't necessarily initialize and new A() default-initializes.
That is not true. Refer to:
Originally Posted by C++11 Clause 8.5 Paragraph 6 (part)
To default-initialize an object of type T means:
— if T is a (possibly cv-qualified) class type (Clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
Originally Posted by C++11 Clause 8.5 Paragraph 7 (part)
To value-initialize an object of type T means:
— if T is a (possibly cv-qualified) class type (Clause 9) with a user-provided 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 (possibly cv-qualified) non-union class type without a user-provided constructor, then the object is zero-initialized and, if T's implicitly-declared default constructor is non-trivial, that constructor is called.
So, if the class does not have a default constructor, then new A() still value initialises, not default initialises. This matters since value initialisation would at least mean zero initialisation, whereas default initialisation could effectively leave certain members uninitialised.
Furthermore, if we take my example of the class having a virtual function, then in combination with:
Originally Posted by C++11 Clause 12.1 Paragraph 5 (part)
A default constructor is trivial if it is not user-provided and if:
— its class has no virtual functions (10.3) and no virtual base classes (10.1)
We see that since "T's implicitly-declared default constructor is non-trivial, that constructor is called".
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|