CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    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.

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    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:

    Code:
    A a();
    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.

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Is there any difference between these two statements?

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Jul 2013
    Posts
    576

    Re: Is there any difference between these two statements?

    Quote Originally Posted by LarryChen View Post
    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Is there any difference between these two statements?

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Jul 2013
    Posts
    576

    Re: Is there any difference between these two statements?

    Quote Originally Posted by laserlight View Post
    , 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.

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Is there any difference between these two statements?

    Quote Originally Posted by laserlight View Post
    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.

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Is there any difference between these two statements?

    Quote 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.

    Quote 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:
    Quote 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);
    Quote 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:
    Quote 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".
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured