共用方式為


Rules for Declaring Constructors 

A constructor has the same name as its class. Any number of constructors can be declared, subject to the rules of overloaded functions. (For more information, see Overloading.)

class-name  (  argument-declaration-list  )

Remarks

The argument-declaration-list may be empty.

C++ defines two special kinds of constructors, default and copy constructors, described in the following table.

Default and Copy Constructors

Kind of Construction Arguments Purpose

Default constructor

Can be called with no arguments

Construct a default object of the class type

Copy constructor

Can accept a single argument of reference to same class type

Copy objects of the class type

Default constructors can be called with no arguments. However, you can declare a default constructor with an argument list, provided all arguments have defaults. Similarly, copy constructors must accept a single argument of reference to the same class type. More arguments can be supplied, provided all subsequent arguments have defaults.

If you do not supply any constructors, the compiler attempts to generate a default constructor. If you do not supply a copy constructor, the compiler attempts to generate one. These compiler-generated constructors are considered public member functions. An error is generated if you specify a copy constructor with a first argument that is an object and not a reference.

A compiler-generated default constructor sets up the object (initializes vftables and vbtables, as described previously), and it calls the default constructors for base classes and members, but it takes no other action. Base class and member constructors are called only if they exist, are accessible, and are unambiguous.

A compiler-generated copy constructor sets up a new object and performs a memberwise copy of the contents of the object to be copied. If base class or member constructors exist, they are called; otherwise, bitwise copying is performed.

If all base and member classes of a class type have copy constructors that accept a const argument, the compiler-generated copy constructor accepts a single argument of type const type**&. Otherwise, the compiler-generated copy constructor accepts a single argument of type type&**.

You can use a constructor to initialize a const or volatile object, but the constructor itself cannot be declared as const or volatile. The only legal storage class for a constructor is inline; use of any other storage-class modifier, including the __declspec keyword, with a constructor causes a compiler error.

The stdcall calling convention is used on static member functions and global functions declared with the __stdcall keyword, and that do not use a variable argument list. When you use the __stdcall keyword on a non-static member function, such as a constructor, the compiler will use the thiscall calling convention."

Constructors of base classes are not inherited by derived classes. When an object of derived class type is created, it is constructed starting with the base class components; then it moves to the derived class components. The compiler uses each base class's constructor as that part of the complete object is initialized (except in cases of virtual derivation, as described in Initializing Base Classes).

See Also

Reference

Constructors (C++)