C ++ notes -- array objects, vector objects, and constructors

Source: Internet
Author: User

I. array objects and vector objects
Account * pact = new account [10]
1. Allocate an array containing 10 account class objects in the heap;
2. Use the account class default constructor to initialize
Release * pact. Do not use Delete pact. Only the first class object calls the destructor. Delete [] pact should be used;
Vector <point> VEC (5); // I
Initialization Process
1. Create a temporary object of the underlying type and apply the default constructor to it;
2. Apply the copy constructor in sequence on each element of the vector, and initialize each class object with the copy of the temporary class object;
3. Delete temporary class objects;

I-type Initialization is costly and is not recommended. As a general design principle, perform the following operations:
Vector <point> CVS;
Int cv_cnt = cal_control_vertices ();
// Reserved memory to store the cv_cnt ponit object
// CVS is still empty
Cvs. Reserve (cv_cnt );
// Open a file and iterate it;
Ifstream infile ("spritemodel ");
Istream_iterator <point> cvfile (infile), Eos;
// Insert element now
Copy (cvfile, EOS, inserter (CVS, cvs. Begin ()));
II. The differences between the value assignment of data members in the initialization table and the constructor are as follows: Code :
Inline account: accoutn (const char * Name, double opening_bal)
: _ Name (name), _ balance (openingbal)
{
_ Acct_nmbr = get_unique_acct_nmbr ();
}
And
Inline account: accoutn (const char * Name, double opening_bal)
{
_ Name = Name;
_ Balance = openingbal;
_ Acct_nmbr = get_unique_acct_nmbr ();
}
1. The results of the two implementations are the same;
2. difference: the member initialization table only provides initialization for the data members of this type, while the constructor sets a value for the data members;
Specifically, we can divide the execution process of the constructor into two stages: implicit or display initialization stage and general computing stage. The computing phase consists of all the statements in the function body.
In the computing phase, the data member settings are considered as a value assignment instead of initialization;
It is determined by implicit or display initialization and whether there is a member initialization table. The sequence of implicit initialization call is to first call the base class function and then call the class member constructor. The following code:
Inlien account: Account ()
{
_ Name = "";
_ Balance = 0.0;
_ Acct_nmbr = 0;
}
During the initialization phase, _ name has called its default structure function, so you do not need to repeat the value assignment in the class initialization function;
For built-in non-data member _ balance and _ acc_nmbr, the results of the two initialization methods are similar in performance, but more popular is to use the member initialization table:
Inline account: Account (): _ balane (0.0), _ acct_nmbr (0)
{}
With two exceptions, const and reference data members must be initialized in the member initialization table. Both const and reference data must have occurred when the constructor starts initialization.
Tip: the initialization sequence in the member initialization table is right-> left

Rest

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.