C++ vs Java constructors

前端 未结 14 2462
一个人的身影
一个人的身影 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:25

    For C++ types that declare constructors, it is not possible to create instances of those types without the use of a constructor. For example:

    class A {
       public:
          A( int x ) : n( x ) {}
      private:
          int n;
    };
    

    it is not posible to create instancev of A without using the A(int) constructor, except by copying, which in this instance will use the synthesised copy constructor. In either case, a constructor must be used.

提交回复
热议问题