C++ vs Java constructors

前端 未结 14 2464
一个人的身影
一个人的身影 2021-02-09 20:31

According to John C. Mitchell - Concepts in programming languages,

[...] Java guarantees that a constructor is called whenever an object is created.

相关标签:
14条回答
  • 2021-02-09 21:32

    As far as I remember, Meyers in his "Effective C++" says, that the object is ONLY created when the control flow has reached his constructor's end. Otherwise it is not an object. Whenever you want to mistreat some raw memory for an actual object, you can do this:

    class SomeClass
    {
       int Foo, int Bar;
    };
    
    SomeClass* createButNotConstruct()
    {
       char * ptrMem = new char[ sizeof(SomeClass) ];
       return reinterpret_cast<SomeClass*>(ptrMem);
    }
    

    You won't hit any constructors here, but you may think, that you are operating a newly created object (and have a great time debugging it);

    0 讨论(0)
  • 2021-02-09 21:34

    There are particular cases in C++ where a constructor will not be called. In particular for POD types the implicitly defined default constructor will not be called in some situations.

    struct X {
       int x;
    };
    int main() {
       X x;        // implicit default constructor not called
                   //    No guarantee in the value of x.x
       X x1 = X(); // creates a temporary, calls its default constructor
                   //    and copies that into x1. x1.x is guaranteed to be 0
    }
    

    I don't quite remember the whole set of situations where that can happen, but I seem to recall that it was mostly in this case.

    To further address the issue:

    This is pointed as a Java peculiarity which makes it different from C++ in its behaviour. So I must argue that C++ in some cases does not call any constructor for a class even if an object for that class is created.

    Yes, with POD types you can instantiate objects and no constructor will be called. And the reason is

    This is of course done for compatibility with C.

    (as Neil comments out)

    I think that this happens when inheritance occurs, but I cannot figure out an example for that case.

    This has nothing to do with inheritance, but with the type of object being instantiated.

    0 讨论(0)
  • 2021-02-09 21:36

    Java constructors can call another constructor of the same class. In C++ that is impossible. http://www.parashift.com/c++-faq-lite/ctors.html

    POD's (plain old data types) are not initialized via constructors in C++:

    struct SimpleClass {
        int m_nNumber;
        double m_fAnother;
    };
    
    SimpleClass simpleobj = { 0 }; 
    SimpleClass simpleobj2 = { 1, 0.5 }; 
    

    In both cases no constructor is called, not even a generated default constructor:

    • A non-const POD object declared with no initializer has an "indeterminate initial value".
    • Default initialization of a POD object is zero initialization. ( http://www.fnal.gov/docs/working-groups/fpcltf/Pkg/ISOcxx/doc/POD.html )

    If however, SimpleClass itself defined a constructor, SimpleClass would not be a POD anymore and one of the constructors would always be called.

    0 讨论(0)
提交回复
热议问题