C + + notes: Constructors __jquery

Source: Internet
Author: User

Constructors are special member functions that perform constructors whenever a new object of the class type is created. There is no limit to the number of constructors declared by a class. As long as the formal parameter list of each constructor is unique, the argument determines which constructor constructor to use cannot be declared as const, as with any member function, the constructor can define the use of default arguments either inside or outside the class to reduce duplication of code
execution of ConstructorsInitialization phase: An implicit or explicit initialization phase is either explicit or implicit, depending on the existence of a member initialization table
The implicit initialization phase calls the default constructors of all base classes in the order in which they are declared, followed by the default constructors for all Member class objects. Members of a built-in type are not implicitly initialized to assign values to data members of a class type or to use an initializer in a general computational phase that is equivalent to both results and performance: The structure of the statement in the body of a constructor
constructor Initialization listA member of a class type that does not have a default constructor that must be initialized with a constructor (cannot create an object assignment)
Members of a const type (cannot be assigned) a member of a reference type (which must be initialized) the initialization order of the member initialization list specifies only the values that are used to initialize the member, and does not specify the order in which the initialization is performed.
The order in which members are initialized is to define the order of members and assignment operations within constructors the setting value of a member in a constructor is an assignment operation, not an initialization operation, whose initialization is already in the initialization table. Constructor initialization list calls a class-matching constructor, where assignment operations first invoke the class default constructor to initialize the object, and then perform an assignment operation on the invocation of the assignment constructor. (see Code1.1 below). The execution of the constructor begins with the initialization phasedefault constructorconstructors that provide default arguments for all parameters also define a default constructor the compiler automatically synthesizes a default constructor if a class does not have a constructor defined to initialize a member with a class type by running its own default constructor.
Members of the built-in and composite types, pointers and arrays are initialized only for objects defined in the global scope, and are not initialized in local scopes if a class contains members of a built-in or composite type such as pointers and arrays, the class should not rely on the resultant default constructor. It should define its own constructors to initialize these members. Class should typically define each constructor of each class with a default constructor that has a NoDefault member, and you must explicitly initialize the NoDefault member by passing an initial string value to the NoDefault constructor.
The compiler will not synthesize a default constructor for classes that have NoDefault type members. If such a class wants to provide a default constructor, it must be explicitly defined, and the default constructor must explicitly initialize its NoDefault member. The NoDefault type cannot be used as an element type for dynamically allocating an array nodefault a static allocation array of types must provide an explicit initializer for each element. If you have a container that holds the NoDefault object, such as vector, you cannot use a constructor that accepts the container size without providing an element initializer at the same time. Use the default constructor My_class MyObj;
My_class myobj = My_class ();implicit class-Type conversionsA constructor that can be invoked with a single argument defines an implicit conversion from the parameter type to the class type unless there is a clear reason to define an implicit conversion, the single parameter constructor should be explicit. ITEM.SAME_ISBN (Null_book); Error:string constructor is explicit set the constructor to explicit to avoid errors, and when the conversion is useful, users can explicitly construct the object. ITEM.SAME_ISBN (Sales_item (Null_book));

Explicit initialization of a class member that does not have a constructor defined and all its data members are public can initialize its members in the same manner as the initialization array element, depending on the declaration order of the data member. ATA Val2 = {1024, "Anna Livia Plurabelle"}; This form of initialization inherits from C and supports compatibility with C programs. All data members of the class are required to be public
to place the burden on each member of the initialization object on the programmer. Error prone, because it is easy to forget the initialization or provide an improper initialization type. If you add or remove a member, you must find all the initialization and correctly update the difference between the code1.1  initialization list and the assignment operation within the constructor

#include <iostream> #include <stdio.h> using namespace std;  Class Book {Public:book (const string &str = ' ", double num = 0.0): ISBN (str), price (num) {cout <<" Book::init " << Endl;
Book (const book &book): ISBN (BOOK.ISBN), Price (Book.price) {cout << "book::copy" << Endl;} book& operator= (const book &book) {cout << ' book::= ' << endl; isbn = book.isbn; price = Book.price; r
Eturn *this;
} private:string ISBN;
Double Price;

}; Class MyTest {public:mytest (const book &book): Books (book), num (0.0) {cout << "mytest::init:" << Endl; MyTest (): Books ("0-990-09", 2.2), num (0.0) {cout << "MyTest::()" << Endl; }/* MyTest (const book &book) {cout << ' mytest::init ' << endl; books = book; num = 0.0;} */Private:b
Ook books;
Double num;

int main () {string ISBN ("9-999-9999"); cout << "Create book===" << Endl;
Book book (isbn,1.1); cout << "Create test===" <&Lt
MyTest test (book);
cout << "Create test1===" << Endl;

MyTest test1;
return 0; }

constructor Initialization list calls a class-matched constructor:
Create book===
Create test===
Create test1=== book
:: Init

The assignment operation within the constructor first invokes the class default constructor to initialize the object, and then the assignment constructor is invoked to assign the operation:

Modify the constructor to

MyTest (const book &book)
cout << "Mytest::init" << Endl;
Books = Book;
num = 0.0;

Create book===
Create test===
Create test1===

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.