现在的位置: 首页 > 综合 > 正文

default constructor

2012年10月06日 ⁄ 综合 ⁄ 共 3740字 ⁄ 字号 评论关闭

https://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fcplr376.htm

default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values.

If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a default parameterless
constructor A::A(). This constructor is an inline public member of its class. The compiler will implicitly define A::A() when
the compiler uses this constructor to create an object of type A. The constructor will have no constructor initializer and a null body.

The compiler first implicitly defines the implicitly declared constructors of the base classes and nonstatic data members of a class A before
defining the implicitly declared constructor of A. No default constructor is created for a class that has any constant or reference type members.

http://en.wikipedia.org/wiki/Default_constructor

In C++, the standard describes the default constructor for a class as a constructor that
can be called with no arguments (this includes a constructor whose parameters all have default arguments).[1] For
example:

class MyClass
{
  public:
    MyClass();             // constructor declared
 
  private:
    int x;
};
 
MyClass :: MyClass()       // constructor defined   
{
    x = 100;
}
 
int main()
{
    MyClass object;        // object created 
}                          // => default constructor called automatically

Allocating memory dynamically, the constructor may be called by adding parenthesis after the dynamic object. In a sense, this is an explicit call to the constructor.

int main()
{
    MyClass * pointer = new MyClass();  // object created 
}                                       // => default constructor called automatically

If the constructor is declared with one or more parameters which all have default values, then it is still a default constructor. Remember that there could be only one default constructor in your class, may it be with default parameters or without any parameters.

    MyClass (int i = 0) {}

In C++, default constructors are significant because they are automatically invoked in certain circumstances:

  • When an object value is declared with no argument list, e.g. MyClass x;; or allocated dynamically with no argument list, e.g. new
    MyClass
     or new MyClass(); the default constructor is used to initialize the object
  • When an array of objects is declared, e.g. MyClass x[10];; or allocated dynamically, e.g. new
    MyClass [10]
    ; the default constructor is used to initialize all the elements
  • When a derived class constructor does not explicitly call the base class constructor in its initializer list, the default constructor for the base class is called
  • When a class constructor does not explicitly call the constructor of one of its object-valued fields in its initializer list, the default constructor for the field's class is called
  • In the standard library, certain containers "fill in" values using the default constructor when the value is not given explicitly, e.g. vector<MyClass>(10);initializes
    the vector with 10 elements, which are filled with the default-constructed value of our type.

In the above circumstances, it is an error if the class does not have a default constructor.

The compiler will implicitly define a default constructor if no constructors are explicitly defined for a class. This implicitly declared default constructor is equivalent to a default constructor defined with a blank body. For example:

class MyClass
{
    int x;                 // no constructor          
};                         // => the compiler produces an (implicit) default constructor
int main()
{
    MyClass object;        // no error: the (implicit) default constructor is called
}

[2]

If some constructors are defined, but they are all non-default, the compiler will not implicitly define a default constructor. Hence, a default constructor may not exist for a class.This is the reason for a typical error which can be demonstrated by the following
example.

class MyClass 
{
  public:
    MyClass (int y);         // a constructor     
 
  private:
    int x;
};
MyClass :: MyClass (int y)
{
    x = y;
}
int main()
{
    MyClass object(100);     // constructor called
    MyClass *pointer;        // for declaration do not need to know about existing constructors
    pointer = new MyClass(); // error: no default constructor    
    return 0;
}

As a constructor of type other than default is defined the compiler does not define a default constructor and hence the creation of object_2 leads to an error.[3]

抱歉!评论已关闭.